Hi Guys ! why my simple state handling isn't worki...
# compose
m
Hi Guys ! why my simple state handling isn't working ?
Copy code
class Core {
    val texts:MutableList<String> = mutableListOf("Salam !","Hello World !")
    fun add (text:String):Core{
        texts.add(text)
        return this
    }
}
fun main() = Window {
    var core by remember { mutableStateOf(Core()) }
    MaterialTheme {
        Button(onClick = {
            core = core.add("salam ! salam !")
        }) {
            Column {
                core.texts.map {
                    Text (it)
                }
            } } } 
}
f
Because you are not changing the state, only mutating it's property
❤️ 1
Change the
texts
property to
Copy code
val texts:MutableList<String> = mutableStateListOf("Salam !","Hello World !")
And the core
val
to
Copy code
var core = remember { Core() }
👍 1
❤️ 1
c
This is still one of the hardest things for me to remember. Wish there was like a lint rule or something that prevented me from using mutable objects with compose.
2
❤️ 1
m
@Filip WiesnerThanks so much ! so as i thought we can have only String or (int,Boolean) as a State , not a class or data class.
f
State can be anything 😉 It clicked for me when I stopped thinking about
remember { mutableStateOf() }
as a one thing and actually started using
remember {}
alone. One of the first things you learn when starting with Compose is using
remember
with
mutableState
together and it was hard for me to separate it after you learn what
remember
really does. Hope it makes sense... 😅
🙌 1
❤️ 1
m
@Filip Wiesner Thanks !!! You saved my day !
f
No worries, good luck coding 🎉