Resources and fetching state kind
Just wondering is it okay to fetch with resource then use that to set state, for example on a store or even a reactive map from @solid-primitives/map. Or should the custom storage config option be used. I saw the example with create reactive deep signal on https://www.solidjs.com/docs/latest/api#createresource but that's just stores as I understand.
wondering if storage option is useful rather than effect or something on resource and if it is any one can help to try and explain how could go about writing the value for map storage option.
SolidJS
Solid is a purely reactive library. It was designed from the ground up with a reactive core. It's influenced by reactive principles developed by previous libraries.
11 Replies
If you want to have a fine-grained resource, the storage option is for you. I just merged createDeepSignal to
@solid-primitives/resource
, it's slightly optimized from the docs version.
And yes, that's exactly how to do it. You either derive your state from the resource or use it directly.Nice thanks will look at the primitive, don't understand however why it would not give the granular updates if I create a store or reactive map for example and just use the resource to populate the initial values. Or does it just mean the resource itself will not continue receiving granular updates?
No, without the storage option, the resource is immutable and will notify all tracked effects.
If you only need one or two parts from the resource as fine-grained reactive, consider createMemo, otherwise the storage option is a good fit.
Alright I think I get how I was thinking about it wrong, basically no point in using resource just to fetch right? If going to use a store anyway to access values, can just fetch normally onMount or something and add to store.
Resources have a few additional benefits, like error and suspense handling. They are a good way to get promises into the signaling world of solid.
You can also use your existing store to create the deep signal used in the storage option.
But in that case, you need to access the resource in your effects at least once, otherwise it will never fetch, as it is lazily evaluated.
Cool yea I'd like to try and use resource, but the type has to match the signature of signal right?
Like this doesnt really make sense right?
rough draft of what I was thinking if I want to fetch entries for a map
This would be the storage value for the resource
Actually, that would work, but only provide reactivity on the topmost layer.
It's probably better to fill the reactive map from a resource using a computation.
It's probably a good idea not to think too much in patterns, but rather ask the question "what is the least amount of reactivity that I can get away with?" – the answer is usually the most idiomatic solid code.
Okay cool, so could just fetch the resource, and maybe createEffect would work to update based on resource?
For example, if the number of map entries is fixed, you can use a normal map and fill it with signals or memos.
computation just means anything like onMount, effects, etc..
createComputed has immediate timing, unlike createEffect.