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

Akhil Jain

07/22/2022, 10:52 AM
Hello guys. I have a small doubt. In the below code, I have written a function
AudioWidgetItem
and I am passing a lambda
onPostClicked
in it. Now when I am invoking this lambda on
line 149
then this function is getting recomposed again and again on some action (even though hash code of this lambda is coming same as printed in
line 144
). But when I am commenting out this lambda invocation at
line 149
then unnecessary recomposition of this function doesn't happen. I am not able to understand the reason behind this behaviour
s

Stylianos Gakis

07/22/2022, 10:42 PM
Is there a chance your onPostClicked changes some state which is hoisted out which results in this function being called again since one of the other inputs has changed?
a

Akhil Jain

07/24/2022, 5:09 PM
Nope. I debugged more and found that actionData is the culprit here. If I am wrapping actionData in some stable wrapper class and passing that to
AudioWidgetItem
and
onPostClicked
then problems solves
s

Stylianos Gakis

07/24/2022, 5:13 PM
Okay so
JsonElement
was not Stable. Is that from kotlinx.serialization? Also did you need to make onPostClicked stable too? I actually don’t know if lambda functions are inferred as stable or not.
a

Akhil Jain

07/24/2022, 5:23 PM
Its from
gson
Not sure if we can make lambdas as stable. I had to use remember block with lambda to keep its instance same or it'll create different hashcode each time if I use it directly
n

natario1

07/24/2022, 5:26 PM
Do you happen to have a simple repro that you can share? We struggle a lot with lambda recompositions and it would be nice to have a playground to investigate deeper.
2 Views