https://kotlinlang.org logo
#compose
Title
# compose
z

Zoltan Demant

01/19/2023, 3:16 PM
Theres a recommendation to "delay" the read of a value, for example
Modifier.graphicsLayer { alpha = state.value }
in order to reduce the recomposition scope. I feel like that doesnt matter when using a Modifier? Example in 🧵!
Copy code
private fun Modifier.visibility(
    visible: Boolean
): Modifier {
    return composed {
        val alphaState = animateFloatAsState(
            target = if (visible) 1f else 0f
        )

        graphicsLayer {
            alpha = alphaState.value
        }
    }
}
I might as well do this?
Copy code
val alphaState by animateFloatAsState(
    target = if (visible) 1f else 0f
)
Because the recomposition scope doesnt change if I "delay" the read. Am I understanding that right?
For clarity.. I mean doing the
by
thing inside the Modifier.composed block 🙂
r

robercoding

01/19/2023, 3:25 PM
When using
by
, the
alphaState
will still be read where it's used, so in this case inside
graphicsLayer
So in this case it doesn't matter, you're "still deferring the read", so all good 👍
So you're skipping recomposition here
a

Albert Chang

01/20/2023, 2:19 AM
Yeah, using
=
and
by
is completely the same in terms of recomposition scope.
4 Views