createStore

stable

Create 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) => TRequired

Either 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 state
increment: () => set({ count: get().count + 1 }),
// Use functional updates for atomic changes
decrement: () => set((s) => ({ count: s.count - 1 })),
// Access other state properties within actions
resetIfHigh: () => {
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 Update
store.set((s) => ({ a: s.a + 1 })); // { a: 11, b: 2 }
// 3. Full Replacement
store.set({ a: 0 }, true); // { a: 0 }

Related Pages