Statekit - v2.0.1
    Preparing search index...

    Function createStoreContext

    • 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.

      Type Parameters

      • TArgs extends unknown[]
      • TState extends object
      • TActions extends object

      Parameters

      Returns StoreContext<TArgs, TState, TActions>

      A store context object with the given instantiation function.

      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>
      );
      }