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

setStore

Imperatively updates a store's state from outside an action. Useful for integrations, middleware, or non-component code.

Signatures

// Mutator function
function setStore<T>(
  store: Store<T>,
  mutator: (draft: State<T>) => void
): void

// Partial state merge
function setStore<T>(
  store: Store<T>,
  partial: Partial<State<T>>
): void

Parameters

ParameterTypeDescription
storeStore<T>The store to update.
mutator(draft) => voidA function that receives a mutable draft and modifies it.
partialPartial<State<T>>An object to shallow-merge into the current state.

Examples

examples.ts
import { setStore } from 'stroid'
import { myStore } from './store'

// Using a mutator
setStore(myStore, (s) => {
  s.count = 0
  s.items = []
})

// Using partial merge
setStore(myStore, { count: 0 })

// Reset to initial state
setStore(myStore, myStore.initialState)

// Conditional updates
setStore(myStore, (s) => {
  if (s.count > 10) {
    s.count = 10  // cap at 10
  }
})