As described in Reducers, a Redux reducer function:
(previousState, action) => newState
, similar to the type of function you would pass to Array.prototype.reduce(reducer, ?initialValue)
Date.now
or Math.random
).Note on immutability, side effects, and mutation
Mutation is discouraged because it generally breaks time-travel debugging, and React Redux's
connect
function:
- For time traveling, the Redux DevTools expect that replaying recorded actions would output a state value, but not change anything else. Side effects like mutation or asynchronous behavior will cause time travel to alter behavior between steps, breaking the application.
- For React Redux,
connect
checks to see if the props returned from amapStateToProps
function have changed in order to determine if a component needs to update. To improve performance,connect
takes some shortcuts that rely on the state being immutable, and uses shallow reference equality checks to detect changes. This means that changes made to objects and arrays by direct mutation will not be detected, and components will not re-render.Other side effects like generating unique IDs or timestamps in a reducer also make the code unpredictable and harder to debug and test.
Because of these rules, it's important that the following core concepts are fully understood before moving on to other specific techniques for organizing Redux reducers:
Key concepts:
Reading list:
combineReducers
combineReducers
combineReducers
Key Concepts:
Reading List:
Key Concepts:
Reading List:
Key Concepts:
Reading List:
© 2015–2017 Dan Abramov
Licensed under the MIT License.
http://redux.js.org/docs/recipes/reducers/PrerequisiteConcepts.html