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

Simon Stahl

03/08/2022, 6:03 PM
Reposting for visibility: is there a way to know what triggered a re-compose? If you have a complex page with many states, it gets really hard to figure out what triggered it.
r

ritesh

03/08/2022, 7:40 PM
I wish there was or if there is one. In cases where it's deeply nested and i am not sure, i end up adding print/log statement!
s

Simon Stahl

03/08/2022, 8:11 PM
the thing is a state can trigger a recomposition without actually changing its value. That make it virtually impossible find out which state is responsible 😞
c

Casey Brooks

03/09/2022, 12:21 AM
The default implementation of
mutableStateOf
uses a
structuralEqualityPolicy()
, which should not trigger recomposition if the state value is updated but does not actually change. So a starting point might be to figure out where you're using a different policy
s

Simon Stahl

03/09/2022, 12:58 AM
I don't think i use any equality policies other than the default. I have to check my code again, but if i remember right, one issue was with either
bottomSheetState.progress
or with
lazyListState.layoutInfo
. I believe either the
progress
or the
layoutInfo
triggers a state update if the instance changes internally even though it contains the same values as before. I got partially around this by wrapping them in a
derivedStateOf
, but this doesn't feel ideal.
Thanks for the hint though, that should give me something to drill down on. Would be great if states had something like an
onEach{}
like flows that you can see when they trigger
e

efemoney

03/09/2022, 9:54 AM
You can wrap any snapshot state in a
snapshotFlow
that you observe in the same manner
☝️ 1
s

Simon Stahl

03/09/2022, 4:45 PM
right, just to observe their emits. I assume that this will change their emit behavior though. Also it adds an unnecessary conversion from state to flow back to state, but i'll try it for debug purposes.
What i meant with above comment was that it would be nice to have something build into the states directly that would allow us to see their emits
3 Views