Перейти к основному содержимому

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

  1. event Event
  2. defaultState (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

Try it

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

  1. effect Effect
  2. defaultState (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

Try it

restore(obj)

Creates an object with stores from object with values

Arguments

  1. 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

Try it