Billy Newman
08/10/2021, 7:02 PMColton Idle
08/10/2021, 7:04 PMBilly Newman
08/10/2021, 7:17 PM@Composable
fun Stuff(state: StuffState) {
val values = state.values?.filter { it.visible == true }
...
ValuesContent(values)
}
@Composable
fun ValuesContent(values: List<Stuff>) {
values.forEach { stuff ->
// show stuff here...
}
}
I modify the state in the viewModel by replacing the entire list with a new list.
val newValues = values.toMutableList() // this should create a new list
newValues.find { /* update some object */ }?.let {
it.visible = false
}
state.values = newValues
I am pretty sure if a new list is created a recompose should trigger. Is this still true? Something else I am doing wrong?Ravi
08/10/2021, 8:10 PMsnapshotFlow { state.values }.collect { … }
Billy Newman
08/10/2021, 8:34 PM@Composable
fun HelloContent(name: String) { ... }
The name string is immutable here, if it changes a recompose will happen to update any views in the HelloContent composable.
@Composable
fun HelloContent(names: List<String>) { ... }
The name list here is immutable as well, if it changes shouldn’t a recompose fire to update the view?Caution: Using mutable objects such as ArrayList<T> or mutableListOf() as state in Compose will cause your users to see incorrect or stale data in your app.
Mutable objects that are not observable, such as ArrayList<T> or a mutable data class, cannot be observed by Compose to trigger recomposition when they change.
Instead of using non-observable mutable objects, we recommend you use an observable data holder such as State<List<T>> and the immutable listOf().
Ravi
08/10/2021, 8:50 PMBilly Newman
08/10/2021, 8:51 PM