Why does a Redux reducer have to be side-effect free?

Viewed 2239

I have been using Redux in my React app and something has been bothering me. The documentation for Redux makes it very clear that the reducer should be state free. You often see examples like this:

function reducer(state = { exampleState: true }, action) {
  switch(action.type) {
  case "ACTION_EXAMPLE":
    return Object.assign({}, state, { exampleState: false });
  default:
    return state;
  }
}

My question is why is this required at all? JavaScript is single threaded. There is no chance of a race condition inside the reducer. As far as I can tell, a Redux store is only capable of returning the current state of the store, so it seems weird that there is so much focus on pure functions.

3 Answers
Related