Conditional fetching with createResource

Hi! Alright, this might be a little bit silly but here's my problem: I have a context provider component that fetches some data to be used later, with createResource. But I'm passing a boolean to the context provider as props, and I'd like the data to be fetched only if the boolean is set to true. What would be the best way to handle this? I'll provide some code if needed but really it's just a simple createResource calling the simplest fetcher. Thank you in advance, have a nice day!
3 Replies
Tommypop
Tommypop2y ago
You can use the boolean as the resource source, and the data should only be fetched if it's true:
const [data] = createResource(booleanValue, fetcher);
const [data] = createResource(booleanValue, fetcher);
If the booleanValue is a signal, it'll automatically fetch if it's set to true
Nyantastic
NyantasticOP2y ago
Wow, it works perfectly, thank you!
Tommypop
Tommypop2y ago
You're welcome :)
Want results from more Discord servers?
Add your server