class TestData(val test: String)
@Composable
fun StableTest() {
var state by remember { mutableStateOf(TestData("Marko")) }
SideEffect {
println(state.test)
}
Text(
modifier = Modifier
.clickable { state = TestData("Marko") }
.padding(32.dp),
text = state.test,
)
}
this triggers recomposition,
TestData
is not stable, but
data class TestData(val test: String)
doesn’t. Is there a reason to additionally mark
data class
with
@Stable
? or is
@Stable
kind of forcing stability? but adding
@Stable
to non
data class
version still triggers recomposition
a
Albert Chang
04/06/2021, 8:23 AM
I don't think this has anything to do with
@Stable
.
@Stable
may matters when you directly use the class as the parameter of a composable function. Here you are using a `State`'s value so the difference is because of the results of
equals
.
m
Marko Novakovic
04/06/2021, 9:24 AM
I see, thanks
Marko Novakovic
04/06/2021, 9:30 AM
@Albert Chang how would you modify this example to showcase how
@Stable
works?
a
Albert Chang
04/06/2021, 10:26 AM
Copy code
data class TestData(var test: String)
@Composable
fun StableTest() {
var count by remember { mutableStateOf(0) }
StableTest(data = TestData("TestData"), count = count) { count++ }
}
@Composable
fun StableTest(data: TestData, count: Int, onClick: () -> Unit) {
Column {
StableTest(data)
Button(onClick = onClick) {
Text(text = count.toString())
}
}
}
@Composable
fun StableTest(data: TestData) {
println("Function called: ${System.currentTimeMillis()}")
Text(text = data.test)
}