amar_1995
11/22/2019, 8:04 AM+memo
, +state
, +model
and when to use which one ?
I know all of this observing some value and try to recompose on value changes.Yann Badoual
11/22/2019, 8:23 AMstate
and model
are using memo
, they're just convenience wrappersYann Badoual
11/22/2019, 8:23 AMfun <T> state( init: () -> T) = memo { State(init()) }
Yann Badoual
11/22/2019, 8:24 AMfun <T> model(init: () -> T) = memo { init() }
Yann Badoual
11/22/2019, 8:25 AMThe State class is an @Model class meant to wrap around a single value
Yann Badoual
11/22/2019, 8:31 AMstate
, if you have multiple values and grouping them makes sense, you can use model
for that (with a @Model
annotated class).amar_1995
11/22/2019, 9:44 AM+memo
and +model
Yann Badoual
11/22/2019, 9:55 AMYann Badoual
11/22/2019, 9:56 AMmodel
callsamar_1995
11/22/2019, 9:56 AMamar_1995
11/22/2019, 11:57 AM+stateFor
take multiple value and so how to access that values and change it.
Let say I use this +stateFor<V1,V2> { something }
As per of doc It says An effect to introduce a state value of type T into a composition that will last as long as the inputs v1 and v2 do not change.
So how to acccess this values and change it accordingly.Yann Badoual
11/22/2019, 12:53 PMuserCounter
state, eg:
val userCounter = +state { 0 }
and everytime you press a button, you'll increment this value, and a textfield will get updated somewhere.
Now let's say that the user can change, and if that's the case, the counter should reset to 0.
You could do something like
val userCounter = +stateFor(userId) { 0 }
With this, when userId changes, the counter will reset.
This example is a bit simplified, and could be replaced by adding a counter
property in a User
model insteadYann Badoual
11/22/2019, 12:55 PMmemo
actually does)