Interview frequency: …
Short answer
It is the process of automatically re-invoking composable functions when their state changes to update the screen, while skipping the components whose inputs have not changed.
Explanation
Recomposition is intelligent (intelligent skipping); Compose compares the new parameters with the old ones, and if they are equal and stable, it skips running the function body entirely. Recomposition can happen at high frequency, and composable functions must not contain any side effects directly in their body.
Code example
@Composable
fun Counter() {
var count by remember { mutableStateOf(0) }
Button(onClick = { count++ }) {
Text("Count: $count") // Only this part recomposes when it changes count
}
}Common mistakes
- Putting expensive computations or creating new objects inside the body of a composable without using remember, causing them to repeat on every frame.