Hello, I have some difficulties understanding `by...
# compose
t
Hello, I have some difficulties understanding
by state
and
remember
differences. Could someone guide me towards some documentation about it?
m
there is state codelab.
remember
is tied up to composable itself. it serves to "survive"
recomposition
but when
composable
is removed from the screen remembered value is forgotten 😄.
by state
is used to store
state
and you can use it with observer pattern or
hoist
it to a higher point
t
Thanks. To clarify my understanding.
remember
is limited to the scope of the component. If the component is destroyed, there will be nothing to remember. The state is something external to the component, if the component is destroyed, but recreated later, it will get the same state than the previous destroyed component?
m
Right. And when state changes composable changes
a
t
Thanks! So: • If the state the change, it will refresh the component; • If something remembered change it, it will do nothing; Is that correct? If so, what does
Copy code
remember { mutableStateOf() }
instead of
Copy code
by State {}
by MutableState {}
?
a
mutableStateOf(initialValue) returns MutableState, and remember keeps it from reinitializing to initialValue
if you don’t use remember the mutableState always get reset to the initial value on every recompose
t
So changing the mutable state will cause a re-composition, and in the re-composed component, remember will just reuse the previous mutablestate instance used (the one that caused the recomposition), with the latest value used.
a
yes
🙏 1
t
So, what are the delegations here for?
a
convenience.
Copy code
val mutableState = remember { mutableStateOf(false) }
mutableState.value = true

var state by remember { mutableStateOf(false) }
state = true