StoreOptions
The optional second argument to createStore. Configures persistence, cross-tab synchronization, DevTools, SSR behaviour, validation, and lifecycle hooks.
Type Definition
interface StoreOptions<State = any> {
persist?: boolean | PersistConfig
sync?: boolean | SyncOptions
devtools?: boolean
historyLimit?: number // default 50
version?: number
migrations?: Record<number, (state: State) => State>
schema?: unknown
validator?: (next: State) => boolean
middleware?: Array<(ctx: MiddlewareCtx) => void | State>
onSet?: (prev: State, next: State) => void
onReset?: (prev: State, next: State) => void
onDelete?: (prev: State) => void
onCreate?: (initial: State) => void
onError?: (err: string) => void
redactor?: (state: State) => State
allowSSRGlobalStore?: boolean
}
interface PersistConfig {
key: string
driver: { getItem?: (k: string) => string | null; setItem?: (k: string, v: string) => void; removeItem?: (k: string) => void }
serialize?: (value: unknown) => string
deserialize?: (value: string) => unknown
encrypt?: (str: string) => string
decrypt?: (str: string) => string
}
interface SyncOptions {
channel?: string
conflictResolver?: ({ local, incoming, localUpdated, incomingUpdated }: any) => any
}persist
| Option | Type | Default | Description |
|---|---|---|---|
| key | string | required | Storage key to use. |
| driver | Storage-like | localStorage | Object implementing getItem/setItem/removeItem. |
| serialize/deserialize | (v)=>string / (str)=>any | JSON | Customize how state is written/read. |
| encrypt/decrypt | (str) => str | undefined | Optional transforms for persisted string payloads. |
Note
version and migrations live at the top level of StoreOptions.sync
Set to true to synchronize state across browser tabs (BroadcastChannel under the hood). Provide an optional channel name and conflictResolver to customize merge strategy.
devtools
| Option | Type | Default | Description |
|---|---|---|---|
| devtools | boolean | false | Connect this store to Redux DevTools. |
| historyLimit | number | 50 | Maximum shallow diffs kept for time travel. |
Full Example
full-options.ts
import { createStore } from 'stroid'
createStore(
'auth',
{ user: null as User | null, token: '', theme: 'dark' },
{
persist: {
key: 'auth-store',
driver: localStorage,
version: 2,
migrate: (old, version) => (version === 1 ? { ...old, theme: 'dark' } : old),
},
sync: true,
devtools: true,
historyLimit: 100,
}
)