Skip to content
Myr

update-patterns.md

This file

Path

docs/update-patterns.md

144 lines · 3.6 KiB · Markdown

Last changed by

Added docs based on the suggestions in #457Michel Weststratebefore adoptionc45f835

History of this file

Take it

Took improvements from #426 and #115, added patterns page to the docsMichel Weststratebefore adoptionedcc658

lines 1–11why, one step back
1---
2id: update-patterns3title: Update patterns4---56<div id="codefund"><!-- fallback content --></div>78Working with immutable data, before Immer, used to mean learning all the immutable update patterns.910To help 'unlearning' those patterns here is an overview how you can leverage the built-in JavaScript APIs to update objects and collections:11

Added docs based on the suggestions in #457Michel Weststratebefore adoptionc45f835

lines 12–13why, one step back
12### Object mutations
13

Took improvements from #426 and #115, added patterns page to the docsMichel Weststratebefore adoptionedcc658

lines 14–35why, one step back
14```javascript
15import produce from "immer"1617const todosObj = {18 id1: {done: false, body: "Take out the trash"},19 id2: {done: false, body: "Check Email"}20}2122// add23const addedTodosObj = produce(todosObj, draft => {24 draft["id3"] = {done: false, body: "Buy bananas"}25})2627// delete28const deletedTodosObj = produce(todosObj, draft => {29 delete draft["id1"]30})3132// update33const updatedTodosObj = produce(todosObj, draft => {34 draft["id1"].done = true35})

Added docs based on the suggestions in #457Michel Weststratebefore adoptionc45f835

line 36why, one step back
36```

Took improvements from #426 and #115, added patterns page to the docsMichel Weststratebefore adoptionedcc658

line 37why, one step back
37

Added docs based on the suggestions in #457Michel Weststratebefore adoptionc45f835

lines 38–42why, one step back
38### Array mutations
3940```javascript41import produce from "immer"42

Took improvements from #426 and #115, added patterns page to the docsMichel Weststratebefore adoptionedcc658

lines 43–94why, one step back
43const todosArray = [
44 {id: "id1", done: false, body: "Take out the trash"},45 {id: "id2", done: false, body: "Check Email"}46]4748// add49const addedTodosArray = produce(todosArray, draft => {50 draft.push({id: "id3", done: false, body: "Buy bananas"})51})5253// delete by index54const deletedTodosArray = produce(todosArray, draft => {55 draft.splice(3 /*the index */, 1)56})5758// update by index59const updatedTodosArray = produce(todosArray, draft => {60 draft[3].done = true61})6263// insert at index64const updatedTodosArray = produce(todosArray, draft => {65 draft.splice(3, 0, {id: "id3", done: false, body: "Buy bananas"})66})6768// remove last item69const updatedTodosArray = produce(todosArray, draft => {70 draft.pop()71})7273// remove first item74const updatedTodosArray = produce(todosArray, draft => {75 draft.shift()76})7778// add item at the beginning of the array79const addedTodosArray = produce(todosArray, draft => {80 draft.unshift({id: "id3", done: false, body: "Buy bananas"})81})8283// delete by id84const deletedTodosArray = produce(todosArray, draft => {85 const index = draft.findIndex(todo => todo.id === "id1")86 if (index !== -1) draft.splice(index, 1)87})8889// update by id90const updatedTodosArray = produce(todosArray, draft => {91 const index = draft.findIndex(todo => todo.id === "id1")92 if (index !== -1) draft[index].done = true93})94

Added docs based on the suggestions in #457Michel Weststratebefore adoptionc45f835

lines 95–109why, one step back
95// filtering items
96const updatedTodosArray = produce(todosArray, draft => {97 // creating a new state is simpler in this example98 // (note that we don't need produce in this case,99 // but as shown below, if the filter is not on the top100 // level produce is still pretty useful)101 return draft.filter(todo => todo.done)102})103```104105### Nested data structures106107```javascript108import produce from "immer"109

Took improvements from #426 and #115, added patterns page to the docsMichel Weststratebefore adoptionedcc658

lines 110–130why, one step back
110// example complex data structure
111const store = {112 users: new Map([113 [114 "17",115 {116 name: "Michel",117 todos: [118 {119 title: "Get coffee",120 done: false121 }122 ]123 }124 ]125 ])126}127128// updating something deeply in-an-object-in-an-array-in-a-map-in-an-object:129const nextStore = produce(store, draft => {130 draft.users.get("17").todos[0].done = true

Added docs based on the suggestions in #457Michel Weststratebefore adoptionc45f835

lines 131–138why, one step back
131})
132133// filtering out all unfinished todo's134const nextStore = produce(store, draft => {135 const user = draft.users.get("17")136 // when filtering, creating a fresh collection is simpler than137 // removing irrelvant items138 user.todos = user.todos.filter(todo => todo.done)

Took improvements from #426 and #115, added patterns page to the docsMichel Weststratebefore adoptionedcc658

lines 139–144why, one step back
139})
140```141142Note that many array operations can be used to insert multiple items at once by passing multiple arguments or using the spread operation: `todos.unshift(...items)`.143144Note that when working with arrays that contain objects that are typically identified by some id, we recommend to use `Map` or index based objects (as shown above) instead of performing frequent find operations, lookup tables perform much better in general.