Skip to content
Myr

example-reducer.md

This file

Path

docs/example-reducer.md

57 lines · 2.2 KiB · Markdown

Last changed by

Immer 7Michel Weststratebefore adoption467ea5d

History of this file

Take it

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 1–7why, one step back
1---
2id: example-reducer3title: Example Reducer4---56<div id="codefund"><!-- fallback content --></div>7

Added egghead.io video embedsMichel Weststratebefore adoptiona866760

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

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

line 12why, one step back
12 <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 13–16why, one step back
13 </div>
14 <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>15</details>16

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 17–21why, one step back
17Here is a simple example of the difference that Immer could make in practice.
1819```javascript20// Redux reducer21// 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 22why, one step back
22const byId = (state = {}, action) => {

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 23–37why, one step back
23 switch (action.type) {
24 case RECEIVE_PRODUCTS:25 return {26 ...state,27 ...action.products.reduce((obj, product) => {28 obj[product.id] = product29 return obj30 }, {})31 }32 default:33 return state34 }35}36```37

Immer 7Michel Weststratebefore adoption467ea5d

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

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 39–49why, one step back
39
40```javascript41import produce from "immer"4243const byId = produce((draft, action) => {44 switch (action.type) {45 case RECEIVE_PRODUCTS:46 action.products.forEach(product => {47 draft[product.id] = product48 })49 }

docs: fix sample code regarding initial stateMichel Weststratebefore adoption30b5b77

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

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 51–52why, one step back
51```
52

Immer 7Michel Weststratebefore adoption467ea5d

line 53why, one step back
53Notice 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 54–56why, one step back
54
55Creating 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).56

Immer 7Michel Weststratebefore adoption467ea5d

line 57why, one step back
57_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._