S
SolidJS15mo ago
kerrick

What should I use if I want features of both Resources and Stores?

I'm using createResource in a RouteDataFunc, which fetches a JSON tree asynchrconously. I love using a Resource here because I get access to its state (to show loading etc). However, I want to be able to mutate said data using the amazing API that Store has, such as function addChild(thingId) { myData.update('things', (t) => t.id === thingId, 'child', { foo: 'bar' }); }. Sadly, that mutation API is only available on Store, not Resource -- and state is only available on Resource, not Store. Is there something that has both? Or is there a built-in API I can use with all the ways Store setters work, but on a Resource?
4 Replies
thetarnav
thetarnav14mo ago
Checkout the storage option of createResource. It will let you save the resource state to something other than a flat signal, eg a store. Then you’ll have access to both store apis for mutation, and resource’s for fetching state
kerrick
kerrick14mo ago
I tried that, but the storage option requires that the setter has the same signature as the setter from createSignal -- therefore there's no ability to pass a series of strings, filter functions, and ranges like there is with a Resource.
thetarnav
thetarnav14mo ago
That is sort of true it can handle any type of params you pass in to mutate it’s just that the resource internally will as a signal setter which you have to account for and reconcile the value but who said you cannot create a store first, then the resource, and pass the store setter to the storage option solely for the purpose of reconciling fetched values. then you can use the original store just fine
thetarnav
thetarnav14mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
Want results from more Discord servers?
Add your server