October 2019
Intermediate to advanced
426 pages
11h 49m
English
Pure functions are functions that, given the same input, will always return the same output. Reducer functions in Redux are pure, so, given the same state and action, they will always return the same new state.
For example, the following reducer is an impure function, because calling the function multiple times with the same input results in a different output:
let i = 0function counterReducer (state, action) { if (action.type === 'INCREMENT') { i++ } return i}console.log(counterReducer(0, { type: 'INCREMENT' })) // prints 1console.log(counterReducer(0, { type: 'INCREMENT' })) // prints 2
To turn this reducer into a pure function, we have to make sure it does not depend on an outside state, ...
Read now
Unlock full access