attach
since
effector 20.13.0
Wrapper for effect, which allows to map effect arguments and use data from stores.
Use cases: declarative way to pass values from stores to effects and argument preprocessing.
Formulae
When newEffect
is called, call mapParams
with params of the newEffect
and data from source
, then call original effect
- If
attach
called withoutsource
,mapParams
will be called only with params of thenewEffect
attach()
always returns new Effect
Short example
Short example with source
Long example
attach({effect, source})
Create effect which will trigger given one with values from source
stores
Arguments
effect
(Effect): Wrapped effectsource
(Store |{[key: string]: Store}
): Store or object with stores, values of which will be passed to the second argument ofmapParams
Returns
Effect: New effect
attach({effect, mapParams})
Create effect which will trigger given one by transforming params by mapParams
function
Arguments
effect
(Effect): Wrapped effectmapParams
((newParams) => effectParams
): Function which receives new params and maps them to the params of the wrappedeffect
. Works like event.prepend. Errors happened inmapParams
function will force attached effect to fail
Returns
Effect: New effect
attach({effect, mapParams, source})
Create effect which will read values from source
stores, pass them with params to mapParams
function and call effect
with result
Arguments
effect
(Effect): Wrapped effectmapParams
((newParams, values) => effectParams
): Function which receives new params and current value ofsource
and combines them to the params of the wrappedeffect
. Errors happened inmapParams
function will force attached effect to failsource
(Store |{[key: string]: Store}
): Store or object with stores, values of which will be passed to the second argument ofmapParams
Returns
Effect: New effect
note
If mapParams
throw an error, it will trigger fail
event and nested effect
will not be called at all
attach({effect})
Create effect which will call effect
with params as it is. That allow to create separate effects with shared behavior.
Arguments
effect
(Effect): Wrapped effect
Returns
Effect: New effect
since
effector 21.5.0