1 Redux Reducer
With the actions firing and the drawing done, it’s time to look at the business logic of our particle generator. We’ll get it done in just 33 lines of code and some change.
Well, it’s a bunch of change. But the 33 lines that make up CREATE_PARTICLES
and TIME_TICK
changes are the most interesting. The rest is just setting various flags.
All of our logic goes in the reducer. Dan Abramov says to think of reducers as the function you’d put in .reduce()
. Given a state and a set of changes, how do I create the new state?
A “sum numbers” example would look like this:
let sum = [1,2,3,4].reduce((sum, n) => sum+n, 0);
For each number, take the previous sum and add the number.
Our particle generator is a more complicated version of the same concept. It takes the current application state, incorporates an action, and returns the new application state.
To ...