As Local Storage
Want a local state instead of global state? Or, want to set the initial state inside component?
const [CatStoreProvider, useCatStoreContext] = withContext(() =>
createStore(({ set }) => ({
age: 0,
isSleeping: false,
increaseAge: () => set((state) => ({ age: state.age + 1 })),
reset: () => set({ age: 0, isSleeping: false }),
})),
);
function Parent() {
return (
<>
<CatStoreProvider>
<CatAge />
<CatIsSleeping />
<WillNotReRenderAsCatStateChanged />
</CatStoreProvider>
<CatStoreProvider>
<CatAge />
<CatIsSleeping />
<WillNotReRenderAsCatStateChanged />
</CatStoreProvider>
<CatStoreProvider onInitialize={(store) => store.set({ age: 99 })}>
<CatAge />
<CatIsSleeping />
<WillNotReRenderAsCatStateChanged />
</CatStoreProvider>
</>
);
}
function CatAge() {
const { age } = useCatStoreContext()((state) => [state.age]);
// Shorthand after v1.13.0:
// const age = useCatStoreContext()('age');
return <div>Age: {age}</div>;
}
function CatIsSleeping() {
const useCatStore = useCatStoreContext();
const { isSleeping } = useCatStore((state) => [state.isSleeping]);
// Shorthand after v1.13.0:
// const isSleeping = useCatStore('isSleeping');
return (
<>
<div>Is Sleeping: {String(isSleeping)}</div>
<button onClick={useCatStore.get().increaseAge}>Increase cat age</button>
</>
);
}