restore
restore(event, defaultState)
Creates a Store from Event.
It works like a shortcut for createStore(defaultState).on(event, (_, payload) => payload)
It is not a derived store
Restore creates a new store. It is not DerivedStore. That means you can modify it's state via events, and use it as target
in sample
.
Arguments
event
EventdefaultState
(Payload)
Returns
Store: New store
Example
import {createEvent, restore} from 'effector'
const event = createEvent()
const $store = restore(event, 'default')
$store.watch(state => console.log('state: ', state))
// state: default
event('foo')
// state: foo
restore(effect, defaultState)
Creates a Store out of successful results of Effect.
It works like a shortcut for createStore(defaultState).on(effect.done, (_, {result}) => result)
Arguments
effect
EffectdefaultState
(Done)
Returns
Store: New store
Example
import {createEffect, restore} from 'effector'
const fx = createEffect(() => 'foo')
const $store = restore(fx, 'default')
$store.watch(state => console.log('state: ', state))
// => state: default
await fx()
// => state: foo
restore(obj)
Creates an object with stores from object with values
Arguments
obj
(State)
Returns
Store: New store
Example
import {restore} from 'effector'
const {foo: $foo, bar: $bar} = restore({
foo: 'foo',
bar: 0,
})
$foo.watch(foo => {
console.log('foo', foo)
})
// => foo 'foo'
$bar.watch(bar => {
console.log('bar', bar)
})
// => bar 0