Skip to content
stroid
Docsapistore options
Docs for v0.0.2 - current npm release. Reduce your code, reduce your stress - never your functionality.

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

OptionTypeDefaultDescription
keystringrequiredStorage key to use.
driverStorage-likelocalStorageObject implementing getItem/setItem/removeItem.
serialize/deserialize(v)=>string / (str)=>anyJSONCustomize how state is written/read.
encrypt/decrypt(str) => strundefinedOptional 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

OptionTypeDefaultDescription
devtoolsbooleanfalseConnect this store to Redux DevTools.
historyLimitnumber50Maximum 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,
  }
)