Store
Store is an object that holds the state value. Store is updated when it receives a value that is not equal (!==) to current one and to undefined
Store Methods#
map(fn: (state: State, lastState?: T) => T)#
Creates a derived store. It will call a provided function with the state, when the original store updates, and will use the result to update the derived store
Formulae#
- When
$firststore is updated, callfnwith new state and previous state - Next update
$secondstore with result offn()call and trigger all subscribers
Arguments
fn(Function): Function that receivesstateandlastState?and returns a new state for the derived store
If the function returns an old state or if it returns undefined, the new store will not be updated.
Returns
Store: New store
Example#
on(trigger, reducer)#
Updates state when trigger is triggered by using reducer. For each trigger, last installed reducer will override previous reducers (useful for dynamic behavior).
Formulae#
- When
triggeris triggered, callreducerwith payload of thetriggerand data of$store - Next update
$storewith result ofreducercall and trigger all subscribers
Arguments
triggerEvent, Effect or another StorereducerReducer: Function that receivesstateandparamsand returns a new state, should be pure. A store cannot hold anundefinedvalue. If a reducer function returnsundefined, the store will not be updated.state: Current state of storeparams: Parameters passed to event call
Returns
Store: Current store
Example#
on(triggers[], reducer)#
since
effector 20.15.0
Updates state when any from triggers is triggered by using reducer.
Formulae#
- When
triggerAortriggerBis triggered, callreducerwith payload of thetriggerAortriggerBand data of$store - Next update
$storewith result ofreducercall and trigger all subscribers - Any count of triggers can be passed to
triggers
Arguments
triggersarray of Event, Effect or StorereducerReducer: Function that receivesstateandparamsand returns a new state, should be pure. A store cannot hold anundefinedvalue. If a reducer function returnsundefined, the store will not be updated.state: Current state of storepayload: Value passed to event/effect call, or source if it passed as trigger
Returns
Store: Current store
Example#
watch(watcher)#
Call watcher function each time when store is updated.
Formulae#
- On initialize and each
$storeupdate, callwatcherwith the new state of$store - When
unwatchis called, stop callingwatcher
Arguments
watcher(Watcher): Watcher function that receives current store state as the first argument
Returns
Subscription: Unsubscribe function
Example#
watch(trigger, watcher)#
Run watcher only when trigger event triggered.
Formulae#
- On each
$storeupdate with passedtrigger, callwatcherwith the new state of$storeand payload fromtrigger - When
unwatchis called, stop callingwatcher
Arguments
triggerEvent, Effect or Store: Trigger, which leads to call ofwatcherwatcher(Function): Function that receives current store state as the first argument and payload of trigger as the second argument.
Returns
Subscription: Unsubscribe function
Example 1#
.watch trigger watcher when foo is executed, because foo is explicitly passed to watch.
First argument of watcher is a state value, second is an event value.
reset(...triggers)#
Resets store state to the default value.
A state is reset when Event or Effect is called or another Store is changed.
Formulae#
- When any unit from
triggerslist is triggered, update$storewith its default state, fromcreateStore(defaultState)
Arguments
Returns
Store: Current store
Example#
reset(triggersArray)#
since
effector 20.15.0
Resets store state to the default value. An overload for arrays of units, which make reset consistent with merge and store.on(triggers[], fn)
A state is reset when Event or Effect is called or another Store is changed.
Formulae#
- When any unit from
triggersArraylist is triggered, update$storewith its default state, fromcreateStore(defaultState)
Arguments
Returns
Store: Current store
Example#
off(trigger)#
- Removes reducer for given
trigger, which was installed via \$store.on or \$store.reset - If there was no reducer for that
trigger, this method will do nothing
Arguments
Returns
Store: Current store
Example#
thru(fn)#
Call function with the given store and return result as it is.
Formulae#
- Call
fnwith$storeas argument - Return result of the
fn()call
Arguments
fn(Function): Function that receivesStoreand returns some value, should be pure
Returns
(any): Value, returned by fn
Example#
Store Properties#
updates#
Formulae#
- When
$storeis changed triggerupdatesevent with the new state
Returns
Event: Event that represents updates of the given store.
Use case: watchers, which will not trigger immediately after creation (unlike store.watch)
shortName#
Returns
(string): ID or short name of the store
defaultState#
Returns
(State): Default state of the store
Example#
Utility methods#
getState()#
Returns current state of store
You don't need this method!
Returns
(State): Current state of the store