What is the difference between these two? ```var a: Boolean by remember { mutableStateOf(false) } va...
p
What is the difference between these two?
Copy code
var a: Boolean by remember { mutableStateOf(false) }
var b: Boolean = remember { false }
m
first one is MutableState<Boolean>, second is just Boolean
remember { false } is kinda pointless i think. If the value is ‘constant’, no point in remembering it i think
also, i think there is no point in mutableState with val also. If you cannot mutate it
to answer the question directly, in terms of behavior as they are defined, i would say they are the same. But i am no expert in compose
👆 1
p
They are meant to be mutable ofc
t
I guess the second one doesn’t trigger a recomposition 🤔
m
ok. then, once the first one value changes, it will recompose (false -> true) and (true - false).
f
Oh, now that you've changed them to
var
it is different. First will recompose on change and second does nothing. And by nothing I mean it's pointless. You could just write
var b = false
👍 1
h
Compose only updates (recompose) its content, when a
State
changes.
remember
keeps the changed
states
between recomposing, otherwise the
State
would be the init state after recomposing.
p
Great answers, thanks! 🙏
z
The second one won't just not cause recomposition, every time it recomposes it will be set to false again if even you changes some previous composition’s “version” of the var. There's nothing special about vars in compose.
remember
is just asking the composition what value its lambda returned the first time it was composed.
p
Why isn't the remember lambda constrained to State?
z
Because there are lots of times when you want to remember something else, eg a mutable state holder that has `MutableState`s inside.
a
remember provides persistence, snapshot state provides transactionality and observability. They're orthogonal.