You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A store is like a signal but it works with deeper objects and retains reactivity for nested updates.
This also allows you to load the data straight from the JSON:
For use inside the context you can either export the store itself:
exportfunctionGlobalContextProvider(props){const[libraryData,setLibraryData]=createStore(JSON.parse(getLibraryData));// set defaults
...
constcontext={
libraryData,
setLibraryData,};return(<GlobalContext.Providervalue={context}>{props.children}</GlobalContext.Provider>);}functionExampleComponent(){constcontext=useGlobalContext();context.libraryData.userSettings.showSideBar;// => booleancontext.setLibraryData("userSettings","showSideBar",false);}
or you could create derived signals and setters for each:
exportfunctionGlobalContextProvider(props){const[libraryData,setLibraryData]=createStore(JSON.parse(getLibraryData));constcontext={showSideBar: ()=>libraryData.userSettings.showSideBar??true,// set defaults here setShowSideBar: (value)=>setLibraryData("userSettings","showSideBar",value),};return(<GlobalContext.Providervalue={context}>{props.children}</GlobalContext.Provider>);}functionExampleComponent(){constcontext=useGlobalContext();context.showSideBar();// => booleancontext.setShowSideBar(true);}
Instead of
(and also instead of below from #5)
you can use a
store
to manage all your options:A store is like a signal but it works with deeper objects and retains reactivity for nested updates.
This also allows you to load the data straight from the JSON:
For use inside the context you can either export the store itself:
or you could create derived signals and setters for each:
Solid docs:
https://docs.solidjs.com/concepts/stores
https://docs.solidjs.com/reference/store-utilities/create-store
Checkout #7 for a better storage interface.
The text was updated successfully, but these errors were encountered: