How to use createEffect with store?

I want to make createEffect when specific property is changed in store. I know how i can do it with createSignal. But how to do it with createStore?
6 Replies
Alex Lohr
Alex Lohr2y ago
createEffect(() => console.log(foo.value)) just referencing value on foo will cause the effect to register to the tracking scope.
musiclover
musicloverOP2y ago
Omg Alex!!! It's so easy than I thought...Solid is amazing....Thanks and much respect for the gods who made Solid... But Alex. I get this warning 'computations created outside a createRoot or render will never be disposed' How to solve this? I have store in a seperate file, not inside any component. And I'm writing createEffect too in seperate file...because there are many side effects I want to run without any components
Alex Lohr
Alex Lohr2y ago
Just wrap it in createRoot, ie
const [store, setStore] = createRoot(() => {
...
return [store, setStore]
});
const [store, setStore] = createRoot(() => {
...
return [store, setStore]
});
If you want to return more than 3 values, consider an object instead of a tuple.
musiclover
musicloverOP2y ago
Thanks Alex always you are amazing.... What's the purpose of createRoot actually under the hood?
Alex Lohr
Alex Lohr2y ago
It sets up a tracking scope for reactive computations, i.e. effects, memos etc, for the cases this is not automatically done by render or hydrate. You can also use it to have a self-destruct switch for effects.
createRoot((dispose) => createEffect((i) => {
console.log(i);
if (i > 3) { dispose(); }
return i + 1;
}, 0));
createRoot((dispose) => createEffect((i) => {
console.log(i);
if (i > 3) { dispose(); }
return i + 1;
}, 0));
should log 4 times and then is garbage-collected.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server