|
| 1 | +# Prerequisite Reducer Concepts |
| 2 | + |
| 3 | + |
| 4 | +As described in [Reducers](../../basics/Reducers.md), a Redux reducer function: |
| 5 | + |
| 6 | +- Should have a signature of `(previousState, action) => newState`, similar to the type of function you would pass to [`Array.prototype.reduce(reducer, ?initialValue)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) |
| 7 | +- Should be "pure", which means it does not mutate its arguments, perform side effects like API calls or modifying values outside of the function, or call non-pure functions like `Date.now()` or `Math.random()`. This also means that updates should be done in an ***"immutable"*** fashion, which means **always returning new objects with the updated data**, rather than directly modifying the original state tree in-place. |
| 8 | + |
| 9 | +>##### Note on immutability, side effects, and mutation |
| 10 | +> Mutation is discouraged because it generally breaks time-travel debugging, and React Redux's `connect` function: |
| 11 | +> - For time traveling, the Redux DevTools expect that replaying recorded actions would output a state value, but not change anything else. **Side effects like mutation or asynchronous behavior will cause time travel to alter behavior between steps, breaking the application**. |
| 12 | +> - For React Redux, `connect` checks to see if the props returned from a `mapStateToProps` function have changed in order to determine if a component needs to update. To improve performance, `connect` takes some shortcuts that rely on the state being immutable, and uses shallow reference equality checks to detect changes. This means that **changes made to objects and arrays by direct mutation will not be detected, and components will not re-render**. |
| 13 | +> |
| 14 | +> Other side effects like generating unique IDs or timestamps in a reducer also make the code unpredictable and harder to debug and test. |
| 15 | +
|
| 16 | + |
| 17 | +Because of these rules, it's important that the following core concepts are fully understood before moving on to other specific techniques for organizing Redux reducers: |
| 18 | + |
| 19 | +#### Redux Reducer Basics |
| 20 | + |
| 21 | +**Key concepts**: |
| 22 | + |
| 23 | +- Thinking in terms of state and state shape |
| 24 | +- Delegating update responsibility by slice of state (*reducer composition*) |
| 25 | +- Higher order reducers |
| 26 | +- Defining reducer initial state |
| 27 | + |
| 28 | +**Reading list**: |
| 29 | + |
| 30 | +- [Redux Docs: Reducers](../../basics/Reducers.md) |
| 31 | +- [Redux Docs: Reducing Boilerplate](../ReducingBoilerplate.md) |
| 32 | +- [Redux Docs: Implementing Undo History](../ImplementingUndoHistory.md) |
| 33 | +- [Redux Docs: `combineReducers`](../../api/combineReducers.md) |
| 34 | +- [The Power of Higher-Order Reducers](http://slides.com/omnidan/hor#/) |
| 35 | +- [Stack Overflow: Store initial state and `combineReducers`](http://stackoverflow.com/questions/33749759/read-stores-initial-state-in-redux-reducer) |
| 36 | +- [Stack Overflow: State key names and `combineReducers`](http://stackoverflow.com/questions/35667775/state-in-redux-react-app-has-a-property-with-the-name-of-the-reducer) |
| 37 | + |
| 38 | + |
| 39 | +#### Pure Functions and Side Effects |
| 40 | + |
| 41 | +**Key Concepts**: |
| 42 | + |
| 43 | +- Side effects |
| 44 | +- Pure functions |
| 45 | +- How to think in terms of combining functions |
| 46 | + |
| 47 | +**Reading List**: |
| 48 | + |
| 49 | +- [The Little Idea of Functional Programming](http://jaysoo.ca/2016/01/13/functional-programming-little-ideas/) |
| 50 | +- [Understanding Programmatic Side Effects](http://web24studios.com/2015/10/understanding-programmatic-side-effects/) |
| 51 | +- [Learning Functional Programming in Javascript](https://youtu.be/e-5obm1G_FY) |
| 52 | +- [An Introduction to Reasonably Pure Functional Programming](https://www.sitepoint.com/an-introduction-to-reasonably-pure-functional-programming/) |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +#### Immutable Data Management |
| 57 | + |
| 58 | +**Key Concepts**: |
| 59 | + |
| 60 | +- Mutability vs immutability |
| 61 | +- Immutably updating objects and arrays safely |
| 62 | +- Avoiding functions and statements that mutate state |
| 63 | + |
| 64 | +**Reading List**: |
| 65 | + |
| 66 | +- [Pros and Cons of Using Immutability With React](http://reactkungfu.com/2015/08/pros-and-cons-of-using-immutability-with-react-js/) |
| 67 | +- [Javascript and Immutability](http://t4d.io/javascript-and-immutability/) |
| 68 | +- [Immutable Data using ES6 and Beyond](http://wecodetheweb.com/2016/02/12/immutable-javascript-using-es6-and-beyond/) |
| 69 | +- [Immutable Data from Scratch](https://ryanfunduk.com/articles/immutable-data-from-scratch/) |
| 70 | +- [Redux Docs: Using the Object Spread Operator](../UsingObjectSpreadOperator.md) |
| 71 | + |
| 72 | + |
| 73 | +#### Normalizing Data |
| 74 | + |
| 75 | +**Key Concepts**: |
| 76 | + |
| 77 | +- Database structure and organization |
| 78 | +- Splitting relational/nested data up into separate tables |
| 79 | +- Storing a single definition for a given item |
| 80 | +- Referring to items by IDs |
| 81 | +- Using objects keyed by item IDs as lookup tables, and arrays of IDs to track ordering |
| 82 | +- Associating items in relationships |
| 83 | + |
| 84 | + |
| 85 | +**Reading List**: |
| 86 | + |
| 87 | +- [Database Normalization in Simple English](http://www.essentialsql.com/get-ready-to-learn-sql-database-normalization-explained-in-simple-english/) |
| 88 | +- [Idiomatic Redux: Normalizing the State Shape](https://egghead.io/lessons/javascript-redux-normalizing-the-state-shape) |
| 89 | +- [Normalizr Documentation](https://github.com/paularmstrong/normalizr) |
| 90 | +- [Redux Without Profanity: Normalizr](https://tonyhb.gitbooks.io/redux-without-profanity/content/normalizer.html) |
| 91 | +- [Querying a Redux Store](https://medium.com/@adamrackis/querying-a-redux-store-37db8c7f3b0f) |
| 92 | +- [Wikipedia: Associative Entity](https://en.wikipedia.org/wiki/Associative_entity) |
| 93 | +- [Database Design: Many-to-Many](http://www.tomjewett.com/dbdesign/dbdesign.php?page=manymany.php) |
0 commit comments