createStore
stableCreate a state store
Creates a new store instance. Re-exported from qortex-store for convenience.
Signature
createStore<T>(initializer: T | StateCreator<T>): Store<T>
Parameters
initializer
T | (set, get) => TRequiredEither a raw initial state object or a function that receives set and get and returns the initial state object.
Returns
Returns
Store<T>A store instance with get, set, subscribe, and destroy methods
Examples
Raw Object
The simplest way to create a store for data-only state:
import { createStore } from 'qortex-store-react';const userStore = createStore({name: 'John',loggedIn: false});// Use in React// const state = useStore(userStore);
Initializer Function (Set/Get)
Use an initializer to define actions that have access to current state and updates:
import { createStore } from 'qortex-store-react';const counterStore = createStore((set, get) => ({count: 0,// Use get() to read current stateincrement: () => set({ count: get().count + 1 }),// Use functional updates for atomic changesdecrement: () => set((s) => ({ count: s.count - 1 })),// Access other state properties within actionsresetIfHigh: () => {if (get().count > 10) set({ count: 0 });}}));
set() — All Variants
set supports partial merging, functional updates, and full replacement:
const store = createStore({ a: 1, b: 2 });// 1. Partial Merge (default)store.set({ a: 10 }); // { a: 10, b: 2 }// 2. Functional Updatestore.set((s) => ({ a: s.a + 1 })); // { a: 11, b: 2 }// 3. Full Replacementstore.set({ a: 0 }, true); // { a: 0 }