From f9c311362876345ce28050606d5618e33890be55 Mon Sep 17 00:00:00 2001 From: Matt van Voorst <135849594+matt-miro@users.noreply.github.com> Date: Tue, 25 Jun 2024 19:49:13 +0200 Subject: [PATCH 1/3] Add an update pattern that explains how to replace & update objects in bulk through mutation --- website/docs/update-patterns.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/website/docs/update-patterns.md b/website/docs/update-patterns.md index a239ff18..81820692 100644 --- a/website/docs/update-patterns.md +++ b/website/docs/update-patterns.md @@ -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: "Check Email"} + }) +}) ``` +Please keep in mind that when updating (nested) objects you should do so through mutation. Creating a new object and directly assigning it to a property in your immer draft object, will not work. This creates a new reference and does not update the existing object. + ### Array mutations ```javascript From b45d92183c39b0e77cadf8419c2a53cde4f5aff7 Mon Sep 17 00:00:00 2001 From: Matt van Voorst <135849594+matt-miro@users.noreply.github.com> Date: Tue, 25 Jun 2024 19:53:53 +0200 Subject: [PATCH 2/3] Changed the body for the third todo --- website/docs/update-patterns.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/update-patterns.md b/website/docs/update-patterns.md index 81820692..75060215 100644 --- a/website/docs/update-patterns.md +++ b/website/docs/update-patterns.md @@ -41,7 +41,7 @@ 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: "Check Email"} + id3: {done: true, body: "Feed my cat"} }) }) ``` From 7671d8e9a46d28dc5643e46a8831ad0c20c76828 Mon Sep 17 00:00:00 2001 From: Matt van Voorst <135849594+matt-miro@users.noreply.github.com> Date: Tue, 25 Jun 2024 20:09:57 +0200 Subject: [PATCH 3/3] Updated note with input from Mark Erikson --- website/docs/update-patterns.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/update-patterns.md b/website/docs/update-patterns.md index 75060215..e29e968c 100644 --- a/website/docs/update-patterns.md +++ b/website/docs/update-patterns.md @@ -46,7 +46,7 @@ const updatedTodosObj = produce(todosObj, draft => { }) ``` -Please keep in mind that when updating (nested) objects you should do so through mutation. Creating a new object and directly assigning it to a property in your immer draft object, will not work. This creates a new reference and does not update the existing object. +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