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 fileTake it
Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 1–5why, one step back1---2id: example-reducer3title: Example Reducer4---5docs: updated sponsor linksMichel Weststratebefore adoptionf97d037
lines 6–8why, one step back6<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 back9Added egghead.io video embedsMichel Weststratebefore adoptiona866760
lines 10–13why, one step back10<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 back14 <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 back15 </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>18Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 19–21why, one step back19Here is a simple example of the difference that Immer could make in practice.2021```javascriptchore(docs): Update example-reducer.md (#734)Conrad Magnus Kirschnerbefore adoption5379cdd
line 22why, one step back22// Reducer with inital statedocs: fix typo, INITAL->INITIAL (#741)cheebefore adoption439e4f1
line 23why, one step back23const INITIAL_STATE = {};Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
line 24why, one step back24// Shortened, based on: https://github.com/reactjs/redux/blob/master/examples/shopping-cart/src/reducers/products.jsdocs: fix typo, INITAL->INITIAL (#741)cheebefore adoption439e4f1
line 25why, one step back25const byId = (state = INITIAL_STATE, action) => {Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 26–40why, one step back26 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```40Immer 7Michel Weststratebefore adoption467ea5d
line 41why, one step back41After using Immer, our reducer can be expressed as:Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 42–45why, one step back4243```javascript44import produce from "immer"45chore(docs): Update example-reducer.md (#734)Conrad Magnus Kirschnerbefore adoption5379cdd
line 46why, one step back46// Reducer with inital statedocs: fix typo, INITAL->INITIAL (#741)cheebefore adoption439e4f1
line 47why, one step back47const INITIAL_STATE = {};chore(docs): Update example-reducer.md (#734)Conrad Magnus Kirschnerbefore adoption5379cdd
line 48why, one step back48Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 49–55why, one step back49const 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 back56}, INITIAL_STATE)Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 57–58why, one step back57```58Immer 7Michel Weststratebefore adoption467ea5d
line 59why, one step back59Notice 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 back6061Creating 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).62Immer 7Michel Weststratebefore adoption467ea5d
line 63why, one step back63_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._