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

myanmarking

03/11/2022, 3:35 PM
I’m trying to debug some piece of code. I narrow it down to this click listener:
Copy code
val onItemClicked = { index: Int ->
    listInfoState.animateScrollToItem(index)
}
Why would this cause the excess recomposition i am having, without evening triggering the click ?
wrapping with:
Copy code
val onItemClicked = rememberUpdatedState(
    newValue = { index: Int ->
        listInfoState.animateScrollToItem(index)
    }
)
solves the issue. i don’t understand why
m

mkrussel

03/11/2022, 3:49 PM
Since compose uses equality checks and
onItemClicked
will not be equal to the previous lambda that was created it triggers recomposition. A cleaner option then using
remembeUpdatedState
could be to use method references
listInfoState::animateScrollToItem
m

myanmarking

03/11/2022, 3:50 PM
really? didn’t know that. let me try
m

mkrussel

03/11/2022, 3:50 PM
I read a could article about this but cannot find it again
m

myanmarking

03/11/2022, 3:51 PM
ya, but that won’t work. because the index i need to pass is provided elsewhere
m

mkrussel

03/13/2022, 6:22 PM
Yeah that was the post I was thinking about
m

myanmarking

03/13/2022, 6:35 PM
ya. in this particular case, the issue was that listInfoState was a custom implemented class, and although it was remembered, the class itself was not marked as Stable. That fixed the issue for me
but for sure i learned a lot about this corner cases regarding callbacks. one thing that helped me fix problems was using the
Copy code
recomposeHighlighter
extension property suggested by google play team
i found some weird recompose problems when interacting with completed unrelated elements
6 Views