Skip to content

Commit 5379cdd

Browse files
authoredJan 11, 2021
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) => {
1 parent d3908e1 commit 5379cdd

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed
 

‎docs/example-reducer.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ title: Example Reducer
1919
Here is a simple example of the difference that Immer could make in practice.
2020

2121
```javascript
22-
// Redux reducer
22+
// Reducer with inital state
23+
const INITAL_STATE = {};
2324
// Shortened, based on: https://github.com/reactjs/redux/blob/master/examples/shopping-cart/src/reducers/products.js
24-
const byId = (state = {}, action) => {
25+
const byId = (state = INITAL_STATE, action) => {
2526
switch (action.type) {
2627
case RECEIVE_PRODUCTS:
2728
return {
@@ -42,14 +43,17 @@ After using Immer, our reducer can be expressed as:
4243
```javascript
4344
import produce from "immer"
4445

46+
// Reducer with inital state
47+
const INITAL_STATE = {};
48+
4549
const byId = produce((draft, action) => {
4650
switch (action.type) {
4751
case RECEIVE_PRODUCTS:
4852
action.products.forEach(product => {
4953
draft[product.id] = product
5054
})
5155
}
52-
}, {})
56+
}, INITAL_STATE)
5357
```
5458

5559
Notice that it is not necessary to handle the default case, a producer that doesn't do anything will return the original state.

0 commit comments

Comments
 (0)
Please sign in to comment.