Dispatcher

Dispatcher are async functions that receive a reducer and, in addition some arguments that will be passed to the reducer. This the one way to modify the store. Like react.setState dispatch is async. Use await keyword to make things after a dispatch. There is 2 ways to dispatch:

Dispatching will trigger middlewares and will rerender all connected components that are wrapped by the connect HOC of a same RxStore instance (think singleton).

async dispatch(reducer)

Using dispatch method directly. Be aware with this method. It can generate a lot of boilerplate code. To reduce it, see createDispatchers

Example

todo.reducers.js

import {createTodo} from './todo.utils.js';

export function addTodo(state, todo){
 return {
  ...state, // We don't mutate the state. We create a copy.
  todos: state.todos.concat(createTodo(todo))
 }
}

todo.addInput.container.jsx

createDispatcher(reducer, ...rest)

createDispatcher is more like a built-in helper to reduce repetitive code around dispatch;

Example

todo.reducers.js

todo.dispatchers.js

todo.addInput.container.jsx

createDispatchers(mapReducers:Object<key,reducer>):Object<key, dispatcher>

createDispatchers is more like a built-in helper to reduce repetitive code around createDispatcher.

Example

todo.reducers.js

todo.addInput.container.jsx

Last updated