API Reference
Store Creation Functions
1. createNeutrixStore (Recommended)
unction createNeutrixStore<T extends State>(
initialState: T,
options?: StoreOptions & { provider?: boolean }
):
| StoreHook<T> // if provider=false or omitted
| { store: Store<T>, useStore: Function, Provider: React.FC } // if provider=true
Parameters:
- initialState: Initial state object
- options: Store configuration (see StoreOptions below)
Behavior:
- If provider is omitted or false, returns a hook-based store (Zustand-like).
- If provider: true, returns an object with { store, useStore, Provider }.
// Hook store (simpler)
const useStore = createNeutrixStore({ count: 0 });
// Provider store
const { store, useStore, Provider } = createNeutrixStore({ count: 0 }, { provider: true });
2. createCoreStore (Advanced)
function createCoreStore<T extends State>(
initialState: T,
options?: StoreOptions
): Store<T>
Description:
- Low-level store creation with no React hooks.
- Typically used for SSR or highly customized setups.
StoreOptions
interface StoreOptions {
name?: string; // For DevTools/persistence
devTools?: boolean; // Enable Redux DevTools
persist?: boolean | ((state: any) => any); // localStorage persist
validate?: (state: State) => boolean | string; // validation
migration?: {
version: number;
migrate: (oldState: any) => any;
};
concurrency?: boolean; // match final code in createCoreStore
onDestroy?: () => void;
}
Store Interface
get
get<K extends keyof T>(path: K | string): T[K]
Gets a value from the store using a path string.
Example:
const userName = store.get('user.profile.name')
set
set<K extends keyof T>(path: K | string, value: any): void
Sets a value in the store at the given path.
Example:
store.set('user.profile.name', 'John')
batch
batch(updates: [string, any][]): void
Performs multiple updates atomically.
Example:
store.batch([
['user.name', 'John'],
['user.lastLogin', Date.now()]
])
setState
setState<K extends keyof T>(path: K, value: T[K]): void
Shortcut for .set.
replaceState
replaceState(state: T): void
Replace the entire store state at once.
getState
getState(): T
Return the entire current state as a new object copy.
Advanced Features
computed
computed<R>(path: string, fn: (state: T) => R): ComputedFn<R>
Creates a computed value that updates when dependencies change.
action
action<Args extends any[], Result>(
fn: Action<Args, Result>
): (...args: Args) => Promise<Result>
Defines an async operation that can update state. Returns a function that executes your async logic with the store.
suspend
suspend<R>(promise: Promise<R>): R
Used for integrating with React Suspense. Throws a promise if data not yet resolved.
subscribe
subscribe(subscriber: () => void): () => void
Subscribes to store changes. Returns an unsubscribe function.
React Hooks
useNeutrixSelector
function useNeutrixSelector<S extends State, R>(
selector: (store: Store<S>) => R
): R
Select and subscribe to part of the store using a function.
useNeutrixComputed
function useNeutrixComputed<S extends State, R>(
computeFn: (store: Store<S>) => R
): R
Compute derived values that automatically update when dependencies change.
useNeutrixAction
function useNeutrixAction<S extends State, Args extends any[], R>(
action: (store: Store<S>, ...args: Args) => Promise<R>
): {
loading: boolean;
error: Error | null;
execute: (...args: Args) => Promise<R>;
}
Wraps async store actions to track loading and error states.
Store Connections
connectStores
function connectStores<S extends State, T extends State>(
connections: StoreConnection<S, T>[]
): () => void
Connects multiple stores together, allowing them to react to each other's changes.
StoreConnection Interface
interface StoreConnection<S extends State = State, T extends State = State> {
source: Store<S>;
target: Store<T>;
when: (source: Store<S>) => boolean;
then: (target: Store<T>) => void;
immediate?: boolean;
}
Core Types
type State = Record<string, any>;
interface Middleware {
onSet?: (path: string, value: any, prevValue: any) => any;
onGet?: (path: string, value: any) => any;
onBatch?: (updates: [string, any][]) => [string, any][];
onError?: (error: Error) => void;
}