Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update example-reducer.md #734

Merged
merged 1 commit into from Jan 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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