Learning Redux
上QQ阅读APP看书,第一时间看更新

The main reducer function gets executed

Redux executes the main reducer with the current state and the dispatched action. Usually, the main reducer function will pass substates and the action down to other reducers, which then process the action.

A reducer is just a function, and to get the next state, Redux calls it as follows:

let newState = reducer(previousState, action)

Keep in mind that the reducer function has to be pure. Pure means that it does not cause any side effects, such as modifying the previousState variable. Instead of directly modifying the previous state, a pure reducer function copies data from the previous state to create a new state object.

However, being pure also means that reducers should not perform any other side effects, such as API requests, either. Side effects should be handled by action creators, which then dispatch one or multiple actions.