update-patterns.md
This file
Path
website/docs/update-patterns.md
146 lines · 3.7 KiB · Markdown
Last changed by
docs: unfinished todo filter condition (#1155)Ishrath Inaambefore adoption857b4b7
History of this fileTake it
docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 1–6why, one step back1---2id: update-patterns3title: Update patterns4---56<center>chore: docusaurus upgradeMichel Weststratebefore adoption48a91b3
line 7why, one step back7<div data-ea-publisher="immerjs" data-ea-type="image" className="horizontal bordered"></div>docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 8–16why, one step back8</center>910Working with immutable data, before Immer, used to mean learning all the immutable update patterns.1112To help 'unlearning' those patterns here is an overview how you can leverage the built-in JavaScript APIs to update objects and collections:1314### Object mutations1516```javascriptMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 17why, one step back17import {produce} from "immer"docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 18–42why, one step back1819const todosObj = {20 id1: {done: false, body: "Take out the trash"},21 id2: {done: false, body: "Check Email"}22}2324// add25const addedTodosObj = produce(todosObj, draft => {26 draft["id3"] = {done: false, body: "Buy bananas"}27})2829// delete30const deletedTodosObj = produce(todosObj, draft => {31 delete draft["id1"]32})3334// update35const updatedTodosObj = produce(todosObj, draft => {36 draft["id1"].done = true37})38```3940### Array mutations4142```javascriptMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 43why, one step back43import {produce} from "immer"docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 44–109why, one step back4445const todosArray = [46 {id: "id1", done: false, body: "Take out the trash"},47 {id: "id2", done: false, body: "Check Email"}48]4950// add51const addedTodosArray = produce(todosArray, draft => {52 draft.push({id: "id3", done: false, body: "Buy bananas"})53})5455// delete by index56const deletedTodosArray = produce(todosArray, draft => {57 draft.splice(3 /*the index */, 1)58})5960// update by index61const updatedTodosArray = produce(todosArray, draft => {62 draft[3].done = true63})6465// insert at index66const updatedTodosArray = produce(todosArray, draft => {67 draft.splice(3, 0, {id: "id3", done: false, body: "Buy bananas"})68})6970// remove last item71const updatedTodosArray = produce(todosArray, draft => {72 draft.pop()73})7475// remove first item76const updatedTodosArray = produce(todosArray, draft => {77 draft.shift()78})7980// add item at the beginning of the array81const addedTodosArray = produce(todosArray, draft => {82 draft.unshift({id: "id3", done: false, body: "Buy bananas"})83})8485// delete by id86const deletedTodosArray = produce(todosArray, draft => {87 const index = draft.findIndex(todo => todo.id === "id1")88 if (index !== -1) draft.splice(index, 1)89})9091// update by id92const updatedTodosArray = produce(todosArray, draft => {93 const index = draft.findIndex(todo => todo.id === "id1")94 if (index !== -1) draft[index].done = true95})9697// filtering items98const updatedTodosArray = produce(todosArray, draft => {99 // creating a new state is simpler in this example100 // (note that we don't need produce in this case,101 // but as shown below, if the filter is not on the top102 // level produce is still pretty useful)103 return draft.filter(todo => todo.done)104})105```106107### Nested data structures108109```javascriptMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 110why, one step back110import {produce} from "immer"docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 111–138why, one step back111112// example complex data structure113const store = {114 users: new Map([115 [116 "17",117 {118 name: "Michel",119 todos: [120 {121 title: "Get coffee",122 done: false123 }124 ]125 }126 ]127 ])128}129130// updating something deeply in-an-object-in-an-array-in-a-map-in-an-object:131const nextStore = produce(store, draft => {132 draft.users.get("17").todos[0].done = true133})134135// filtering out all unfinished todo's136const nextStore = produce(store, draft => {137 const user = draft.users.get("17")138 // when filtering, creating a fresh collection is simpler thanchore: typo irrelvant => irrelevant (#866)Yanick Champouxbefore adoptionb6bf265
line 139why, one step back139 // removing irrelevant itemsdocs: unfinished todo filter condition (#1155)Ishrath Inaambefore adoption857b4b7
line 140why, one step back140 user.todos = user.todos.filter(todo => !todo.done)docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 141–146why, one step back141})142```143144Note 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)`.145146Note 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.