Skip to content
Myr

example-reducer.md

This file

Path

docs/example-reducer.md

59 lines · 2.2 KiB · Markdown

Last changed by

docs: updated sponsor linksMichel Weststratebefore adoptionf97d037

History of this file

Take it

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 1–5why, one step back
1---
2id: example-reducer3title: Example Reducer4---5

docs: updated sponsor linksMichel Weststratebefore adoptionf97d037

lines 6–8why, one step back
6<center>
7<div data-ea-publisher="immerjs" data-ea-type="image" class="horizontal bordered"></div>8</center>

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

line 9why, one step back
9

Added egghead.io video embedsMichel Weststratebefore adoptiona866760

lines 10–13why, one step back
10<details>
11 <summary style="color: white; background:#c200c2;padding:5px;margin:5px;border-radius:2px">egghead.io lesson 13: Using Immer in a (React) reducer</summary>12 <br>13 <div style="padding:5px;">

docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585

line 14why, one step back
14 <iframe style="border: none;" width=760 height=427 scrolling="no" src="https://egghead.io/lessons/react-update-immutable-state-with-react-usereducer-through-immer/embed" ></iframe>

Added egghead.io video embedsMichel Weststratebefore adoptiona866760

lines 15–18why, one step back
15 </div>
16 <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/react-update-immutable-state-with-react-usereducer-through-immer">Hosted on egghead.io</a>17</details>18

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 19–23why, one step back
19Here is a simple example of the difference that Immer could make in practice.
2021```javascript22// Redux reducer23// Shortened, based on: https://github.com/reactjs/redux/blob/master/examples/shopping-cart/src/reducers/products.js

docs: fix sample code regarding initial stateMichel Weststratebefore adoption30b5b77

line 24why, one step back
24const byId = (state = {}, action) => {

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 25–39why, one step back
25 switch (action.type) {
26 case RECEIVE_PRODUCTS:27 return {28 ...state,29 ...action.products.reduce((obj, product) => {30 obj[product.id] = product31 return obj32 }, {})33 }34 default:35 return state36 }37}38```39

Immer 7Michel Weststratebefore adoption467ea5d

line 40why, one step back
40After using Immer, our reducer can be expressed as:

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 41–51why, one step back
41
42```javascript43import produce from "immer"4445const byId = produce((draft, action) => {46 switch (action.type) {47 case RECEIVE_PRODUCTS:48 action.products.forEach(product => {49 draft[product.id] = product50 })51 }

docs: fix sample code regarding initial stateMichel Weststratebefore adoption30b5b77

line 52why, one step back
52}, {})

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 53–54why, one step back
53```
54

Immer 7Michel Weststratebefore adoption467ea5d

line 55why, one step back
55Notice that it is not necessary to handle the default case, a producer that doesn't do anything will return the original state.

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 56–58why, one step back
56
57Creating Redux reducer is just a sample application of the Immer package. Immer is not just designed to simplify Redux reducers. It can be used in any context where you have an immutable data tree that you want to clone and modify (with structural sharing).58

Immer 7Michel Weststratebefore adoption467ea5d

line 59why, one step back
59_Note: it might be tempting after using producers for a while, to just place `produce` in your root reducer and then pass the draft to each reducer and work directly over said draft. Don't do that. It removes the benefit of using Redux as a system where each reducer is testable as a pure function. Immer is best used when applied to small, individual pieces of logic._