S
SolidJS2mo ago
oc

createResource and createContext

Hey! I want avoid prop-drilling for a large resource that is referenced and used by many leaf components.. is there any recommanded way of settinng up createResource with createContext.. i'm having trouble with useContext, as the leafs are rendered when the provider is not ready with a context value.. what is best-pratice or an idomatic way to combine these two primitives together?
3 Replies
bigmistqke
bigmistqke2mo ago
you could either pass the resource, or you could use a getter too:
const [resource] = createResource(...)

return <Context.Provider value={{resource}/>

return <Context.Provider value={{get resource(){ return resource()} }/>
const [resource] = createResource(...)

return <Context.Provider value={{resource}/>

return <Context.Provider value={{get resource(){ return resource()} }/>
then in the child components where you consume that context you will have to do checks if the resource is already defined or not
Madaxen86
Madaxen862mo ago
Or use cached functions and createAsync. It handles deduplication, so you can just call the same cached function wherever in the tree and it will fetch only once.
Alex Lohr
Alex Lohr2mo ago
Or use a global resource, wrapped in createRoot.
Want results from more Discord servers?
Add your server