S
SolidJS3mo 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
bigmistqke3mo 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
Madaxen863mo 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 Lohr3mo ago
Or use a global resource, wrapped in createRoot.
Want results from more Discord servers?
Add your server