Please explain me the difference between `+memo`, `+state`, `+model` and when to use which one ? I ...
a
Please explain me the difference between
+memo
,
+state
,
+model
and when to use which one ? I know all of this observing some value and try to recompose on value changes.
y
state
and
model
are using
memo
, they're just convenience wrappers
Copy code
fun <T> state( init: () -> T) =     memo { State(init()) }
Copy code
fun <T> model(init: () -> T) = memo { init() }
And
The State class is an @Model class meant to wrap around a single value
So if you just want to cache a single value, you can use
state
, if you have multiple values and grouping them makes sense, you can use
model
for that (with a
@Model
annotated class).
a
And what is the difference in
+memo
and
+model
y
From the implementation above none it seems, it's just an alias
Or maybe the compiler is doing something different with
model
calls
a
Okay
Can you explain me
+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.
y
Let's say you have a
userCounter
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 instead
(I highly recommend that you read this awesome article http://intelligiblebabble.com/compose-from-first-principles/ , it explains in a simplified way what
memo
actually does)