```class TestData(val test: String) @Composable fun StableTest() { var state by remember { muta...
m
Copy code
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
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
I see, thanks
@Albert Chang how would you modify this example to showcase how
@Stable
works?
a
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)
}
Add
@Stable
on
TestData
and see the difference.
m
I will try it. thanks @Albert Chang