Skip to content
Myr

example-reducer.md

This file

Path

docs/example-reducer.md

63 lines · 2.4 KiB · Markdown

Last changed by

docs: fix typo, INITAL->INITIAL (#741)cheebefore adoption439e4f1

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–21why, one step back
19Here is a simple example of the difference that Immer could make in practice.
2021```javascript

chore(docs): Update example-reducer.md (#734)Conrad Magnus Kirschnerbefore adoption5379cdd

line 22why, one step back
22// Reducer with inital state

docs: fix typo, INITAL->INITIAL (#741)cheebefore adoption439e4f1

line 23why, one step back
23const INITIAL_STATE = {};

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

line 24why, one step back
24// Shortened, based on: https://github.com/reactjs/redux/blob/master/examples/shopping-cart/src/reducers/products.js

docs: fix typo, INITAL->INITIAL (#741)cheebefore adoption439e4f1

line 25why, one step back
25const byId = (state = INITIAL_STATE, action) => {

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 26–40why, one step back
26 switch (action.type) {
27 case RECEIVE_PRODUCTS:28 return {29 ...state,30 ...action.products.reduce((obj, product) => {31 obj[product.id] = product32 return obj33 }, {})34 }35 default:36 return state37 }38}39```40

Immer 7Michel Weststratebefore adoption467ea5d

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

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 42–45why, one step back
42
43```javascript44import produce from "immer"45

chore(docs): Update example-reducer.md (#734)Conrad Magnus Kirschnerbefore adoption5379cdd

line 46why, one step back
46// Reducer with inital state

docs: fix typo, INITAL->INITIAL (#741)cheebefore adoption439e4f1

line 47why, one step back
47const INITIAL_STATE = {};

chore(docs): Update example-reducer.md (#734)Conrad Magnus Kirschnerbefore adoption5379cdd

line 48why, one step back
48

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 49–55why, one step back
49const byId = produce((draft, action) => {
50 switch (action.type) {51 case RECEIVE_PRODUCTS:52 action.products.forEach(product => {53 draft[product.id] = product54 })55 }

docs: fix typo, INITAL->INITIAL (#741)cheebefore adoption439e4f1

line 56why, one step back
56}, INITIAL_STATE)

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 57–58why, one step back
57```
58

Immer 7Michel Weststratebefore adoption467ea5d

line 59why, one step back
59Notice 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 60–62why, one step back
60
61Creating 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).62

Immer 7Michel Weststratebefore adoption467ea5d

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