vide
08/11/2022, 7:30 AMYou should defer reading state variables as long as possible. Deferring state reads can help ensure that Compose re-runs the minimum possible code on recomposition. For example, if your UI has state that is hoisted high up in the composable tree and you read the state in a child composable, you can wrap the state read in a lambda function. Doing this makes the read occur only when it is actually needed.All other documentation and examples seem to prefer passing only immutable data classes for state -- is this deferring technique recommended only for the most intensive cases (for example animations with values changing every frame)? To my understanding, when a value changes, the whole scope that read it is recomposed, including all child composables (unless the inputs are @Stable in which case all of the inputs will be diffed and recomposed only if the inputs are different). What is the downside of doing deferred reads and passing
State
, how much overhead does it add compared to diffing all parameters of invalidated child composables in a large invalidated scope?
Also, does it matter how the state read is deferred? I have preferred not to use the delegate syntax for mutableStateOf so I can easily pass State<T>
objects directly to child composables. Is there some reason why the provided example uses a callback and not something like this: @Composable fun Child(state: State<T>) { state.value }
?Oliver.O
08/11/2022, 10:34 AMvide
08/11/2022, 10:45 AMvide
08/11/2022, 10:46 AMOliver.O
08/11/2022, 10:49 AM