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 Methodsmap(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
$first
store is updated, callfn
with new state and previous state - Next update
$second
store with result offn()
call and trigger all subscribers
Arguments
fn
(Function): Function that receivesstate
andlastState?
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
#
Exampleon(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
trigger
is triggered, callreducer
with payload of thetrigger
and data of$store
- Next update
$store
with result ofreducer
call and trigger all subscribers
Arguments
trigger
Event, Effect or another Storereducer
Reducer: Function that receivesstate
andparams
and returns a new state, should be pure. A store cannot hold anundefined
value. 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
#
Exampleon(triggers[], reducer)
#
since
effector 20.15.0
Updates state when any from triggers
is triggered by using reducer
.
#
Formulae- When
triggerA
ortriggerB
is triggered, callreducer
with payload of thetriggerA
ortriggerB
and data of$store
- Next update
$store
with result ofreducer
call and trigger all subscribers - Any count of triggers can be passed to
triggers
Arguments
triggers
array of Event, Effect or Storereducer
Reducer: Function that receivesstate
andparams
and returns a new state, should be pure. A store cannot hold anundefined
value. 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
#
Examplewatch(watcher)
#
Call watcher
function each time when store is updated.
#
Formulae- On initialize and each
$store
update, callwatcher
with the new state of$store
- When
unwatch
is called, stop callingwatcher
Arguments
watcher
(Watcher): Watcher function that receives current store state as the first argument
Returns
Subscription: Unsubscribe function
#
Examplewatch(trigger, watcher)
#
Run watcher
only when trigger
event triggered.
#
Formulae- On each
$store
update with passedtrigger
, callwatcher
with the new state of$store
and payload fromtrigger
- When
unwatch
is called, stop callingwatcher
Arguments
trigger
Event, Effect or Store: Trigger, which leads to call ofwatcher
watcher
(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
triggers
list is triggered, update$store
with its default state, fromcreateStore(defaultState)
Arguments
Returns
Store: Current store
#
Examplereset(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
triggersArray
list is triggered, update$store
with its default state, fromcreateStore(defaultState)
Arguments
Returns
Store: Current store
#
Exampleoff(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
#
Examplethru(fn)
#
Call function with the given store and return result as it is.
#
Formulae- Call
fn
with$store
as argument - Return result of the
fn()
call
Arguments
fn
(Function): Function that receivesStore
and returns some value, should be pure
Returns
(any): Value, returned by fn
#
Example#
Store Propertiesupdates
#
#
Formulae- When
$store
is changed triggerupdates
event 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 methodsgetState()
#
Returns current state of store
You don't need this method!
Returns
(State
): Current state of the store