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>,
options?: CreateStoreOptions<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.

options

CreateStoreOptions<T>Default: {}

Optional configuration for the store, including persistence.

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

Automatic Persistence

Enable automatic persistence via qortex-db/store:

import { createDB } from 'qortex-db';
import { createStorePersister } from 'qortex-db/store';
import { createStore } from 'qortex-store-react';
const db = createDB('myapp');
const counterStore = createStore(
{ count: 0 },
{ persister: createStorePersister(db) }
);