From 5379cddef95a237fd25b450e629054b3e02f95ee Mon Sep 17 00:00:00 2001 From: Conrad Magnus Kirschner <35291593+conradkirschner@users.noreply.github.com> Date: Mon, 11 Jan 2021 21:25:10 +0100 Subject: [PATCH] chore(docs): Update example-reducer.md (#734) makes it more eaiser to see, where developers have to move their inital state. prevents some reading issues that creates stuff like: const byId = produce((draft = INITAL_STATE, action) => { --- docs/example-reducer.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/example-reducer.md b/docs/example-reducer.md index e75c519a..701d3804 100644 --- a/docs/example-reducer.md +++ b/docs/example-reducer.md @@ -19,9 +19,10 @@ title: Example Reducer Here is a simple example of the difference that Immer could make in practice. ```javascript -// Redux reducer +// Reducer with inital state +const INITAL_STATE = {}; // Shortened, based on: https://github.com/reactjs/redux/blob/master/examples/shopping-cart/src/reducers/products.js -const byId = (state = {}, action) => { +const byId = (state = INITAL_STATE, action) => { switch (action.type) { case RECEIVE_PRODUCTS: return { @@ -42,6 +43,9 @@ After using Immer, our reducer can be expressed as: ```javascript import produce from "immer" +// Reducer with inital state +const INITAL_STATE = {}; + const byId = produce((draft, action) => { switch (action.type) { case RECEIVE_PRODUCTS: @@ -49,7 +53,7 @@ const byId = produce((draft, action) => { draft[product.id] = product }) } -}, {}) +}, INITAL_STATE) ``` Notice that it is not necessary to handle the default case, a producer that doesn't do anything will return the original state.