-
I am translating a project which use global stores to Solid. Example: // Create store
let store, updateStore, increment
createRoot(() => {
;[store, updateStore] = createStore({ count: 1 })
increment = () => updateStore('count', v => v + 1);
createEffect(() => {
console.log(store.count)
onCleanup(() => {
console.log('cleanup', store.count)
})
})
})
// Use
function Counter() {
return (
<button type="button" onClick={increment}>
{store.count}
</button>
);
} |
Beta Was this translation helpful? Give feedback.
Answered by
ryansolid
Oct 4, 2021
Replies: 1 comment
-
Yes. The important thing to know is that roots can be disposed of. If it's truly a singleton then it probably doesn't matter. But if you ever need to dispose a root it's the function passed to the first argument of createRoot.. You can always hoist it out: let dispose;
createRoot((d) => {
dispose = d;
// reactive stuff
});
// somewhere else when you want to clean up.
dispose(); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
weijarz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes. The important thing to know is that roots can be disposed of. If it's truly a singleton then it probably doesn't matter. But if you ever need to dispose a root it's the function passed to the first argument of createRoot.. You can always hoist it out: