Can we use `remember{mutableStateOf(<data>)}...
# compose
t
Can we use
remember{mutableStateOf(<data>)}
for every type of data?
h
It is not a great idea to use it with inherently mutable data types. Other than that, only major requirement is that the object you are using have a clear
equals
implementation for compose to understand changes.
Copy code
val list by remember { mutableStateOf { mutableListOf(1, 2, 3) } }

list.add(4) // won't trigger recompose
Copy code
var list by remember { mutableStateOf { listOf(1, 2, 3) } }

list = list + 4 // will trigger recompose
t
Got it. Thanks. 👍
z
Also see
mutableStateListOf
,
mutableStateMapOf
, and
derivedStateOf
.
And regarding using those functions directly inside
remember
, considering hoisting your state.
h
To add to that list,
rememberUpdatedState
for callbacks that access a compose state.
t
I will. Laptop closed for now. I have related doubts and opinions regarding stateful/stateless composables to ask. Will ask on the same thread soon.