When Component

Table of Contents


Introduction

Syntax

tml`
	<When hasChanged=stateVariable1>
		${io => (...)}
	</When>
`
tml`
	<When hasChanged="stateVariable1,stateVariable2">
		${io => (...)}
	</When>
`
tml`
	<When hasChanged=${SV.find(/stateVariable\d+/)}>
		${io => (...)}
	</When>
`

The When component re-evaluates its child, a rendering function, whenever any of the state variables or SvMonitor objects specified by the hasChanged attribute change and then mounts the results.
There should be only one child, and it must be a function.

Attributes

The When component ignores the standard attributes.

Children

The When component accepts a single child that must be a function. The function is invoked with the following parameter:

In general, the function should only refer to state variables in the io namespace since changes to those state variables are the only ones that will force the function to be re-evaluated.

The function must return a Template object, a string, a number, a DOM node, a Component object, or an array containing a mix of these types; the same return values as the render() Component instance method.

Usage

Use the When component when the layout of a subtree must change depending on the value of the state variable. Here's an example;

tml`
	<When hasChanged=stateVariable1>
		${io => (
			io.stateVariable1.value > 10
				? tml`<span className=warning>Too large</>`
				: ''
		)}
	</When>
`

The When component will completely discard the generated subtree whenever a change occurs. Use with care if the state variable often changes.

Passing an SvMonitor object instead of a state variable can be useful when used inside another component.
It can also be used to filter when a change is declared by overriding the hasChanged() or isChanged() methods. See SvMonitor Reference.

DOM element properties

The When component is remounted when the hasChanged conditions change.