S
SolidJS2y ago
Max

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
Alex Lohr
Alex Lohr2y ago
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.
Max
MaxOP2y ago
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?
Alex Lohr
Alex Lohr2y ago
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.
Max
MaxOP2y ago
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.
Alex Lohr
Alex Lohr2y ago
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.
Max
MaxOP2y ago
Cool yea I'd like to try and use resource, but the type has to match the signature of signal right?
import { ReactiveMap } from "@solid-primitives/map";

//specific case map of keys and array of values (like many to many kind of data stored in maps)
export function createMapDeepSignal<K,V>(entries?: [K,V[]][]): Signal<[K,V[]][]> {
const reactiveMap = new ReactiveMap<K, V[]>(entries);
return [
reactiveMap,
(update: [K,V[]][]) => {
update.forEach(([key,values])=>{
// when we reach array level probably its normal to lose fine grained reactivity?
reactiveMap.set(key, [...(reactiveMap.get(key)||[]), ...values])
})
}
] as Signal<[K,V[]][]>;
}
import { ReactiveMap } from "@solid-primitives/map";

//specific case map of keys and array of values (like many to many kind of data stored in maps)
export function createMapDeepSignal<K,V>(entries?: [K,V[]][]): Signal<[K,V[]][]> {
const reactiveMap = new ReactiveMap<K, V[]>(entries);
return [
reactiveMap,
(update: [K,V[]][]) => {
update.forEach(([key,values])=>{
// when we reach array level probably its normal to lose fine grained reactivity?
reactiveMap.set(key, [...(reactiveMap.get(key)||[]), ...values])
})
}
] as Signal<[K,V[]][]>;
}
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
Alex Lohr
Alex Lohr2y ago
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.
Max
MaxOP2y ago
Okay cool, so could just fetch the resource, and maybe createEffect would work to update based on resource?
Alex Lohr
Alex Lohr2y ago
For example, if the number of map entries is fixed, you can use a normal map and fill it with signals or memos.
Max
MaxOP2y ago
computation just means anything like onMount, effects, etc..
Alex Lohr
Alex Lohr2y ago
createComputed has immediate timing, unlike createEffect.
Want results from more Discord servers?
Add your server