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

chore: added update-pattern on how to replace & bulk update (nested) objects #1131

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions website/docs/update-patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,19 @@ const deletedTodosObj = produce(todosObj, draft => {
const updatedTodosObj = produce(todosObj, draft => {
draft["id1"].done = true
})

// replace & update in bulk
const updatedTodosObj = produce(todosObj, draft => {
Object.assign(draft, {
id1: {done: true, body: "Take out the trash"},
id2: {done: true, body: "Check Email"},
id3: {done: true, body: "Feed my cat"}
})
})
```

Any time a nested draft field gets a new reference or value, produce() will finish applying the immutable update and return a new reference. If you tried to mutate, but the values remained the same, Immer will bail out and return the existing reference from produce()

### Array mutations

```javascript
Expand Down