I have trouble grasping how recomposition works in...
# compose
m
I have trouble grasping how recomposition works in example I currently have. Code and more info in thread.
Copy code
@Composable
fun ParallaxImage(
    modifier: Modifier = Modifier,
    url: String,
    scrollState: ScrollState,
) {
    SideEffect {
        println("recomposing ParallaxImage")
    }
    val coroutineScope = rememberCoroutineScope()

    var parallaxColor by remember { mutableStateOf(Color.Transparent) }

    var image by remember { mutableStateOf(ImageBitmap(1, 1)) }
    val loader = ImageLoader(LocalContext.current)
    val target = rememberCoilTarget { bitmap ->
        println("setting image")
        image = bitmap.asImageBitmap()
        coroutineScope.launch {
            val color = computeColor(bitmap)
            println("setting parallax color")
            parallaxColor = color
        }
    }

    val request = rememberDefaultImageRequest(url = url, target = target)
    LaunchedEffect(url) {
        loader.execute(request)
    }

    Image(
        modifier = modifier
            .height(250.dp)
            .fillMaxWidth()
            .background(parallaxColor)
            .graphicsLayer { alpha = min(1f, 1 - (scrollState.value / 400f)) },
        bitmap = image,
        contentScale = ContentScale.FillWidth,
        contentDescription = null,
    )
}
log for this is:
Copy code
recomposing ParallaxImage
setting image
recomposing ParallaxImage
setting parallax color
recomposing ParallaxImage
ParallaxImage
is recomposed for every state variable changed inside of it. Is it because those `state`s are used through body of
ParallaxImage
? but again, shouldn’t just composables inside of it be recomposed?
z
m
I read it, that’s why am taking deeper look at what is going on in my code. that was yesterday, I will read it again today
z
parallaxColor
and
image
are read in the
ParallaxImage
function, which is why the entire
ParallaxImage
function gets recomposed when they change
remember that function arguments are evaluated before the function they are being passed to is invoked. They’re evaluated in the stack frame of the function making the call.
m
I understand. thank you very much. that is a great article, I’ve read it again today. now am onto other two
z
is there some way i could make the post more clear for other people asking this question do you think? open to feedback
m
I will think about it
I think only thing you can do to make concept more clear is to add more examples but I don’t think it’s good enough. It will increase size of the article and maybe be too much. I really like the article and I don’t think there is much you can do to make it better
z
There can always be more examples 😅 Thanks for the feedback at any rate!
m
Exactly but question is it worth it. At first I thought example you have in there is tricky(sh) but after reading it 2nd and 3rd time and examining some of my own code everything was clear