Maik
09/17/2020, 9:06 AM@Composable
fun StateDemo() {
var counterOne by remember { mutableStateOf(0) }
var counterTwo by remember { mutableStateOf(0) }
var items = remember { mutableStateListOf<Int>() }
Column {
Button(onClick = { counterOne++ }) {
Text("One")
}
Button(onClick = { counterTwo++ }) {
Text("Two")
}
Button(onClick = { items.add(counterOne) }) {
Text("List")
}
MyText(counterOne)
MyText(counterTwo)
MyList(items)
}
}
@Composable
fun MyText(counter: Int) {
Text("counter: $counter (${System.currentTimeMillis()})")
}
@Composable
fun MyList(items: MutableList<Int>) {
Text("list: ${items.size} (${System.currentTimeMillis()})")
}
Unfortunately, the behavior in case of re-composition is not completely clear to me.
If I click the button "One", the function "*MyText(counterOne)*" is re-drawn, the function "*MyText(counterTwo)*" remains completely unaffected. The behavior with the button "Two" is similar.
Unfortunately in both cases the function "*MyList(items)*" is also redrawn. The behavior is not clear to me.
What am I doing wrong! I want to prevent this function from being drawn, because in my opinion it is not involved in the re-composition.
Many thanks in advance!Se7eN
09/17/2020, 9:21 AMmutableStateOf<List<Int>>()
?Maik
09/17/2020, 9:21 AMSnapshotStateList
.
I know that "Compose" can call the function at any time, I just wanted to keep the parts to be drawn as small as possible for performance reasons.jim
09/17/2020, 9:22 AMList<Int>
does not guarantee the list won't change, it only states avoids the reference holder from being able to call the mutation methods.MutableList
passed into MyList(items)
is going to be a SnapshotStateList
and thus we could be smarter and skip in that situation. I kinda thought we had already implemented that, but maybe we had to turn it off due to bugs in the implementation? Anyway, you could probably file a bug if you'd like, worst case it would be closed as a duplicate if we're already tracking it.Maik
09/17/2020, 9:28 AMChuck Jazdzewski [G]
09/17/2020, 3:57 PM