For all of the Compose Basics course up to this po...
# compose-android
u
For all of the Compose Basics course up to this point, I've been instructed to define state like
Copy code
var amountInput by remember {mutableStateOf()}
However, in a new unit, I'm being directed to write it differently with no explanation.
Copy code
var amountInput: MutableState<String> = mutableStateOf("0")
Is there an appreciable between these two idioms? Are there use-cases specific to each?
m
If this is declared in a
@Composable
function and isn't a field in some class, then the second example is just wrong. It doesn't remember the
MutableState
so it will be recreated every time a recomposition happens.
☝🏻 1
☝️ 2
😬 2
☝🏾 1
☝🏿 1
u
I think maybe you've hit on the answer already! I realize as you say this that maybe there are some states that don't NEED to be remembered between recompositions. To clarify more, this is for the tip calculator project. The var in question is declared outside of a composable. I think its only purpose will be to represent an input in a text field. So in that context, after you've entered your input, the next recomposition likely wouldn't happen until you hit a button or something storing that value elsewhere. Though, not knowing much about user input yet, I wonder if I'm right. Wouldn't recomposition make it impossible to type input longer than a single character? Maybe its impossible to say without the code. I suppose I just have to forge ahead.
Well never mind. This was one big circuitous snipe hunt. The instructions were first showing you the wrong way to do it before introducing remember. The confusing thing is, we've already covered this in the previous unit. Very misleading if you're expecting to build on the knowledge you already have
z
They’re just making sure you’re paying attention 😛
u
You're right that you can never review these concepts too much! It just makes me think I'm going crazy sometimes haha
a
Did those short videos on remember and mutableStateOf() a few years ago Still getting positive comments on these, so I am assuming the explanation (w code) resonates well: remember:

https://www.youtube.com/watch?v=ue8UsCB5RX0

mutableStateOf:

https://www.youtube.com/watch?v=wYaPRmDHkPA

u
Hey thanks! I'll definitely be checking out your channel today. Just giving the first video a glance, I love your to-the-point presentation
c
Also THIS it was the first thing i read that made it click. hope it works for you too. https://blog.zachklipp.com/remember-mutablestateof-a-cheat-sheet/
a
@Uberunix just those two vids about your question. stopped doing youtube for a while now