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

Big Chungus

02/15/2022, 9:52 AM
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

than_

02/15/2022, 10:04 AM
just launch an effect for every state change and do whatever needs to be done there 🙂
b

Big Chungus

02/15/2022, 10:05 AM
As in LaunchedEffect?
b

Big Chungus

02/15/2022, 10:24 AM
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

than_

02/15/2022, 10:29 AM
so you need to actually diff the change?
b

Big Chungus

02/15/2022, 10:32 AM
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

KamilH

02/15/2022, 10:34 AM
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

than_

02/15/2022, 10:56 AM
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

Big Chungus

02/15/2022, 10:57 AM
I'm only interested in c as it's the only one I'm using to invoke external state setter with
a

Alexander Maryanovsky

02/15/2022, 11:23 AM
Pass the state that you want to depend on into another @Composable function, and call the side effect there.
b

Big Chungus

02/15/2022, 11:24 AM
Good idea. I can just write my own effect based on side effect that supports keys that way
a

Adam Powell

02/15/2022, 12:42 PM
What created and owns the thing you're trying to update/manipulate with effects?
b

Big Chungus

02/15/2022, 2:15 PM
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.
2 Views