Has anyone came up with an issue of having to sync...
# compose
b
Has anyone came up with an issue of having to sync compose state to some external state each time it changes on compose side? How did you tackle it?
t
just launch an effect for every state change and do whatever needs to be done there 🙂
b
As in LaunchedEffect?
b
But side effect executes on every recomposition, regardless which piece of state changes. Therefore you cannot pinpoint a particular piece of state and map only its changes to external state.
t
so you need to actually diff the change?
b
Well no. Assume i have states a, b and c. Only c needs to be pushed to external state. Having an effect executing as part of a or b changes will waste the execution time on the effect even though c hasn't changed.
k
Isn’t
snapshotFlow
what you are looking for? It lets you create a flow from state, subscribe to it and collect whenever state changes
t
well that depends. how do you know only C should be pushed? I agree with KamilH that that the solution is probably gonna be a snapshot flow. Some debounce probably won't hurt either.
b
I'm only interested in c as it's the only one I'm using to invoke external state setter with
a
Pass the state that you want to depend on into another @Composable function, and call the side effect there.
b
Good idea. I can just write my own effect based on side effect that supports keys that way
a
What created and owns the thing you're trying to update/manipulate with effects?
b
Here's a minified example of my use-case with comments. Note that
mdcComponent
is only attached to native DOM element because I couldn't think of a better way to make it accessible to the composition scope later on.