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

createStore

createStore(name, initialState, options?) registers a named store. State is plain data; updates are performed later with setStore.

Basic usage

store.ts
import { createStore } from 'stroid'

// Declare once at module scope
createStore('profile', {
  name: 'Alex',
  theme: 'dark',
})

Tip

Call createStore exactly once per store name (module scope is best). Re-calling with the same name is a no-op that preserves state.

Updating state

Mutate through setStore. Drafts are mutable; stroid handles structural sharing under the hood.

update.ts
import { setStore } from 'stroid'

// Object merge
setStore('profile', { theme: 'light' })

// Draft mutation
setStore('profile', (d) => {
  d.name = 'Jordan'
})

Options

Pass options as the third argument. Common ones: persistence, sync, DevTools, SSR controls.

options.ts
import { createStore } from 'stroid'

createStore(
  'settings',
  { theme: 'dark', language: 'en' },
  {
    persist: { key: 'settings', driver: localStorage },
    sync: true,               // cross-tab BroadcastChannel
    devtools: true,           // Redux DevTools
    historyLimit: 50,
  }
)

SSR

In production servers, createStore is blocked to prevent cross-request leaks. Use createStoreForRequest per request, or pass { allowSSRGlobalStore: true } only if you truly want a global store on the server.