Reducer

Reducers are pure functions that receive the current state of the store and arguments and return a new state of the store.

Pure Functions

A pure function is a function which:

  • Given the same input, will always return the same output.

  • Produces no side effects.

Eric Elliotarrow-up-right describes (in this articlearrow-up-right) very well what is the payoff of pure functions.

In this example the classic switch case of redux reducerarrow-up-right disapeared. There is no more combine Reducer.

With RxStore there is no more switch case because we dispatch direclty a reducer to affect the state.

Reducers are destined to be dispatched thought the store.dispatch method. To do so, we can create Dispatchersarrow-up-right.

Example

todo.reducers.js

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

export function removeTodo(state, todo){
 return {
  ...state, // again. 
  todos: state.todos.filter((t) => t.id !== todo.id))
 }
}

but we can imagine a more generic/functional way to split reducers like

common.reducers.js

and rewrigth todo.reducers.js

Last updated