August 2017
Beginner
374 pages
10h 41m
English
Given the same input, pure functions always return the same output. Because reducer functions are pure, given the same state and action, they are always going to return the same new state. This makes them predictable.
The following code defines an impure function, because subsequent calls with the same input result in different output:
var i = 0 function impureCount () { i += 1 return i } console.log(impureCount()) // prints 1 console.log(impureCount()) // prints 2
As you can see, we are accessing a variable outside of the function which is what makes the function impure.
We could make the function pure by specifying i as an argument:
function pureCount (i) { return i + 1 } console.log(pureCount(0)) ...Read now
Unlock full access