Lucas Kivi
05/01/2024, 3:57 PMState
parameters are correctly evaluated by a RecomposeScope
for changes. However, it seems we can get the same performance with a stable lambda based provider pattern. I am not sure by what mechanism that works? Can someone shine some light on that for me?
Example in thread.Lucas Kivi
05/01/2024, 3:58 PM@Composable
fun ComponentState(animatingValueState: State<Int>) {
Row {
CoolTextState(animatingValueState = animatingValueState)
Spacer(modifier = Modifier.weight(1f))
Icon(imageVector = Icons.Rounded.AccountBox)
}
}
@Composable
fun CoolTextState(animatingValueState: State<Int>) {
Text(text = animatingValueState.value.toString())
}
@Composable
fun ComponentProvider(animatingValueProvider: () -> Int) {
Row {
CoolTextProvider(animatingValueProvider = animatingValueProvider)
Spacer(modifier = Modifier.weight(1f))
Icon(imageVector = Icons.Rounded.AccountBox)
}
}
@Composable
fun CoolTextProvider(animatingValueProvider: () -> Int) {
Text(text = animatingValueProvider().toString())
}
Lucas Kivi
05/01/2024, 3:59 PMLucas Kivi
05/01/2024, 9:00 PMStylianos Gakis
05/02/2024, 7:44 AMLucas Kivi
05/02/2024, 3:12 PMState
is obviously a special class that the RecomposeScope
can listen to for changes, but the lambda return value seems to also be “listened to” so that it can intelligently recompose when that return value changes.
I wonder how that behavior plays with the capture’s backing values. What if it is just capturing a State
backed value, what if it is remembered by backed by an unstable value? It looks like I will need to do some testing in order to really understand. I would love to see some docs or an article that go over this.Stylianos Gakis
05/02/2024, 4:40 PMLucas Kivi
05/02/2024, 6:09 PMStylianos Gakis
05/02/2024, 6:27 PMLucas Kivi
05/02/2024, 6:39 PMvar value = "string"
val stringProvider = remember { { value } }
FunComposable(stringProvider)
Would FunComposable
be intelligently recomposed when value
changed?Stylianos Gakis
05/02/2024, 7:08 PMStylianos Gakis
05/02/2024, 7:10 PMLucas Kivi
05/02/2024, 7:25 PMState
.Lucas Kivi
05/02/2024, 7:28 PMState
. The fact that the initial invocation of the lambda returns a State
is what is giving the desired behavior, it is not that the lambda is repeatedly re-evaluated.Stylianos Gakis
05/02/2024, 9:36 PMLucas Kivi
05/02/2024, 10:14 PM