createStore(defaultState)
Creates a store.
Arguments
defaultState
(State): Default state
Returns
(Store
): An object that holds the state tree. There can be multiple stores.
Example
// Create events
const addTodo = createEvent()
const clearTodos = createEvent()
// Create store
const todos = createStore([]) // <-- Default state
todos
// Add reducer
.on(addTodo, (state, todo) => [...state, todo])
// Will reset store to default state when clearTodos is fired
.reset(clearTodos)
// Create derived store
const selectedTodos = todos.map(todos => {
// Filter array inside store
return todos.filter(todo => !!todo.selected)
})
// Handle side effects
todos.watch(state => console.log(state))