Closed sometao closed 6 years ago
Something like this should work:
val state = Var(100)
val body =
<div>
<h1>Hello.</h1>
<button onclick={ () => state.update( s => s + 10) }>+</button>
<button onclick={ () => state.update( s => s - 10) }>-</button>
<div style="display:flex; height:200px">
<div style={ state.map(s => s"width:${s}px; background:red") }></div>
<div style={ state.map(s => s"width:${s}px; background:yellow") }></div>
<div style={ state.map(s => s"width:${s}px; background:blue") }></div>
</div>
</div>
If you need to attach side effects to a Var
it's best to call .impure.run
on it (and manually manage the cancellation than run returns). Going thought map feels a bit like a hack :)
Side effects are what I need.
I will use .impure.run
and handle the cancellation.
Thank you.
Use case:
Some action (or operation) needs binding to a
Var(state)
. When the state changes, the action should be executed.My solution is:
Is there any better solution?