Skip to content

Commit

Permalink
chore(docs): Update example-reducer.md (#734)
Browse files Browse the repository at this point in the history
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) => {
  • Loading branch information
conradkirschner committed Jan 11, 2021
1 parent d3908e1 commit 5379cdd
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions docs/example-reducer.md
Expand Up @@ -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 {
Expand All @@ -42,14 +43,17 @@ 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:
action.products.forEach(product => {
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.
Expand Down

0 comments on commit 5379cdd

Please sign in to comment.