question about stores and effects
o/ so i have a store that stores preferences (dark theme, etc.). i want to update the local storage whenever it changes. rn, i have an effect in the Root() component. i wonder if there's any way to hoist that logic outside
5 Replies
i figured out this works:
but do effects have to be bound to a component?
Yes, you get warnings when using effects outside of components. But why do you need the effect outside? You can just make the store global and run the effect inside root. Or even better, create a Context
Mainly for separation of concerns, I don't want the root to be responsible for updating preferences. But how would a context help here?
Here's how I use it:
I think you can easily have these run in a separate file but you would still need to import it into root or another Higher Order Component.
if you don't care about cleanup (eg the store exists for the whole lifetime of the application) u can just hoist it out and ignore the warning (created outside of
createRoot
)
it is not bound to a component