When Componenttml`
<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.
The When component ignores the standard attributes.
hasChanged
The hasChanged attribute can accept:
The hasChanged attribute will accept the output of SV.find(). Any argument acceptable to SV.find() is acceptable as a value of the hasChanged attribute.
The When component accepts a single child that must be a function. The function is invoked with the following parameter:
iohasChanged, including state variables from any SvMonitors.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.
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.
The When component is remounted when the hasChanged conditions change.
When child function returns template, the Component renders the template.When child function returns an empty string, the Component renders a single <span> with the class 'whenMarker'. This allows the Component to keep a placeholder for any future template nodes.