Creates a store context with an instantiation function. Useful if you need to initialize a store with dynamic data like props, or if you need to create multiple instances of the same store.
import { createStore, createStoreContext } from "@fransek/statekit";import { useMemo } from "react";const StoreContext = createStoreContext((initialCount: number) => createStore({ count: initialCount }, (set) => ({ increment: () => set((state) => ({ count: state.count + 1 })), decrement: () => set((state) => ({ count: state.count - 1 })), reset: () => set({ count: 0 }), })),);function StoreProvider({ children, initialCount,}: { children: React.ReactNode; initialCount: number;}) { const store = useMemo( () => StoreContext.instantiate(initialCount), [initialCount], ); return ( <StoreContext.Provider value={store}>{children}</StoreContext.Provider> );} Copy
import { createStore, createStoreContext } from "@fransek/statekit";import { useMemo } from "react";const StoreContext = createStoreContext((initialCount: number) => createStore({ count: initialCount }, (set) => ({ increment: () => set((state) => ({ count: state.count + 1 })), decrement: () => set((state) => ({ count: state.count - 1 })), reset: () => set({ count: 0 }), })),);function StoreProvider({ children, initialCount,}: { children: React.ReactNode; initialCount: number;}) { const store = useMemo( () => StoreContext.instantiate(initialCount), [initialCount], ); return ( <StoreContext.Provider value={store}>{children}</StoreContext.Provider> );}
A function that returns a new store instance.
A store context object with the given instantiation function.
Creates a store context with an instantiation function. Useful if you need to initialize a store with dynamic data like props, or if you need to create multiple instances of the same store.
Example