Skip to main content

createStore

Method for creating a store

createStore<T>(defaultState: T): Store<T>
createStore<T>(defaultState: T, config: {
name?: string
updateFilter?: (update: T, current: T) => boolean
}): Store<T>

Arguments

  1. defaultState (State): Default state
  2. config (Object): Optional configuration
    • name (String): Name for the store. Babel plugin can set it from the variable name, if not passed explicitly in config.
    • updateFilter (Function): Function which prevent store from update when returns false. Accepts update as first argument and current state as second argument. Redundant for most cases since store already ensure that update is not undefined and not equal (!==) to current state (since effector 21.8.0)

Returns

Store: New store

Example#

import {createEvent, createStore} from 'effector'
const addTodo = createEvent()
const clearTodos = createEvent()
const $todos = createStore([])
// Will update store when addTodo is fired
.on(addTodo, (state, todo) => [...state, todo])
// Will reset store to default state when clearTodos is fired
.reset(clearTodos)
// Create mapped store
const $selectedTodos = $todos.map(todos => {
return todos.filter(todo => !!todo.selected)
})
$todos.watch(state => {
console.log('todos', state)
})
// => todos []

Try it

Last updated on