Reposting for visibility: is there a way to know w...
# compose
s
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
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
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
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
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
You can wrap any snapshot state in a
snapshotFlow
that you observe in the same manner
☝️ 1
s
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