Hi here, Anyone have any idea what is difference b...
# compose-android
d
Hi here, Anyone have any idea what is difference between
skip re-compose
and
do not call composable function
?
Here is the situation: i have a state
Copy code
data class XModelState(
    val name: String,
    val age: Int,
    val intro: String
)
and there is a button with the action to change the
name
and
age
. Debug by
Layout Inspector
, when i click the button, the Composition that using
intro
will skip. But when i derived
intro
of State
Copy code
val intro by remember {
        derivedStateOf {
            modelState.intro
        }
    }
the Composition is not call (not skip neither re-compose).
z
Where does modelState come from, how is its intro property defined?
d
hi Zach, here is the fully code
Copy code
var modelState by remember {
        mutableStateOf(
            XModelState(
                "Default", 3, "Default Intro"
            )
        )
    }
    val age by remember {
        derivedStateOf {
            modelState.age
        }
    }
    val intro by remember {
        derivedStateOf {
            modelState.intro
        }
    }
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {
        println("Column")
        BoxWrapper {
            Text(text = "Name is ${modelState.name}").also {
                println("Name changed")
            }
        }
        BoxWrapper {
            // usecase1: ${modelState.age}, usecase2: $age
            Text(text = "Age is ${modelState.age}").also {
                println("Age changed")
            }
        }
        BoxWrapper {
            // usecase1: ${modelState.intro}, usecase2: $intro
            Text(text = "Intro is $intro").also {
                println("Intro changed")
            }
        }

        Button(
            modifier = Modifier.fillMaxWidth(),
            onClick = {
                modelState = modelState.copy(
                    name = "Dougie ${Random.nextInt(0, 10)}",
                    age = modelState.age + 1
                )
            }
        ) {
            Text(text = "Update Model")
        }
    }
BoxWrapper
makes
Box
noinline to avoid sharing re-compose scope.
From layout inspector, there are 2 situations in skip re-compose number: 1. show a skip times, can see the log
Intro changed
, 2. nothing,
not log
. i wonder to know what is difference between them.