So how do you guys bind a coroutine scope to a Rea...
# javascript
r
So how do you guys bind a coroutine scope to a React component?
To be more precise, how do you cancel the scope associated to a component that gets unmounted, in my case in a functional component. I’m trying to use
useEffectWithCleanup { { scope.cancel() } }
but it gets called on all state changes
z
Have you tried passing in an empty list as the dependencies parameter?
useEffectWithCleanup(listOf()) { scope.cancel() }
If it's anything like
useEffect
, it will be called on every state change unless you pass in an empty list of dependencies.
I've found this to be a good reference on React hooks behavior. https://reactjs.org/docs/hooks-effect.html
r
Yeah I found that and I think it works. I’m still not sure about where everything runs, for example I’m not sure I understand when the
useEffect
cleanup function is called. When I pass an empty list, it should not be called on component updates, just on mount and unmount. And maybe it works because cleanup isn’t called on mount, so it’s effectively only called on unmount. I used this to “get” a component-local coroutine scope https://github.com/Ribesg/Kita/blob/feature/init/modules/client/client-web/src/main/kotlin/fr/ribesg/kita/client/web/components/util/Coroutines.kt#L6