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>>
): voidParameters
| Parameter | Type | Description |
|---|---|---|
| store | Store<T> | The store to update. |
| mutator | (draft) => void | A function that receives a mutable draft and modifies it. |
| partial | Partial<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
}
})