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
defaultState(State): Default stateconfig(Object): Optional configurationname(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 returnsfalse. Accepts update as first argument and current state as second argument. Redundant for most cases since store already ensure that update is notundefinedand not equal (!==) to current state
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 []