https://kotlinlang.org logo
#compose
Title
# compose
t

Tristan

11/30/2020, 2:38 PM
Hello, I have some difficulties understanding
by state
and
remember
differences. Could someone guide me towards some documentation about it?
m

Marko Novakovic

11/30/2020, 2:50 PM
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

Tristan

11/30/2020, 3:27 PM
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

Marko Novakovic

11/30/2020, 3:28 PM
Right. And when state changes composable changes
a

allan.conda

11/30/2020, 3:33 PM
t

Tristan

12/01/2020, 2:36 PM
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

allan.conda

12/01/2020, 3:39 PM
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

Tristan

12/01/2020, 4:09 PM
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

allan.conda

12/01/2020, 4:10 PM
yes
🙏 1
t

Tristan

12/01/2020, 4:11 PM
So, what are the delegations here for?
a

allan.conda

12/01/2020, 4:13 PM
convenience.
Copy code
val mutableState = remember { mutableStateOf(false) }
mutableState.value = true

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