Skip to content

Commit

Permalink
Update example-reducer.md
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 authored Jan 11, 2021
1 parent 7faa7b4 commit 6c23777
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions docs/example-reducer.md
Original file line number Diff line number Diff line change
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 6c23777

Please sign in to comment.