Good pattern for refetching the data

Hi folks, What would be a good way to trigger refetch outside the component? I tried extracting the createResource outside the Tasks component, but that means it'll fetch as soon as component is imported. Is there a better way other than passing down the props? Here's example: https://playground.solidjs.com/anonymous/734acabf-e069-4043-9db2-bf5b014b0691
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
2 Replies
andi
andi14mo ago
So for resources, you can optionally pass 2 arguments instead of one, the second one is the fetching function you have, and the first one can be a function returning a value usually it's an accessor function that would trigger refetches in the resource if that first function returns a falsy value, then the resource won't trigger a fetch so basically if you had something like this:
const [enabled, setEnabled] = createSignal(false);
const [data] = createResource(enabled, fetchSomething);
const [enabled, setEnabled] = createSignal(false);
const [data] = createResource(enabled, fetchSomething);
then the resource wouldn't fetch initially because the enabled signal is false if you later do setEnabled(true) it'll trigger an initial fetch So to answer your original question, you can still extract the resource higher in the component tree, and then only set enabled to true when you want it to fetch initially, for example from the onMount call of one of the children
beganovich
beganovichOP14mo ago
Awesome, thanks Andi!
Want results from more Discord servers?
Add your server