update-patterns.md
This file
Path
website/docs/update-patterns.md
156 lines · 3.8 KiB · Markdown
Last changed by
docs: Update update-patterns.md to resetting/clearing (#1082)siddtheonebefore adoption17ac0b8
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–28why, 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})28docs: Update update-patterns.md to resetting/clearing (#1082)siddtheonebefore adoption17ac0b8
line 29why, one step back29// delete single propertydocs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 30–37why, one step back30const deletedTodosObj = produce(todosObj, draft => {31 delete draft["id1"]32})3334// update35const updatedTodosObj = produce(todosObj, draft => {36 draft["id1"].done = true37})docs: Update update-patterns.md to resetting/clearing (#1082)siddtheonebefore adoption17ac0b8
lines 38–42why, one step back3839// reset/clear/empty40const emptyTodo = produce(todosObj, () => {41 return {};42})docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 43–47why, one step back43```4445### Array mutations4647```javascriptMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 48why, one step back48import {produce} from "immer"docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 49–108why, one step back4950const todosArray = [51 {id: "id1", done: false, body: "Take out the trash"},52 {id: "id2", done: false, body: "Check Email"}53]5455// add56const addedTodosArray = produce(todosArray, draft => {57 draft.push({id: "id3", done: false, body: "Buy bananas"})58})5960// delete by index61const deletedTodosArray = produce(todosArray, draft => {62 draft.splice(3 /*the index */, 1)63})6465// update by index66const updatedTodosArray = produce(todosArray, draft => {67 draft[3].done = true68})6970// insert at index71const updatedTodosArray = produce(todosArray, draft => {72 draft.splice(3, 0, {id: "id3", done: false, body: "Buy bananas"})73})7475// remove last item76const updatedTodosArray = produce(todosArray, draft => {77 draft.pop()78})7980// remove first item81const updatedTodosArray = produce(todosArray, draft => {82 draft.shift()83})8485// add item at the beginning of the array86const addedTodosArray = produce(todosArray, draft => {87 draft.unshift({id: "id3", done: false, body: "Buy bananas"})88})8990// delete by id91const deletedTodosArray = produce(todosArray, draft => {92 const index = draft.findIndex(todo => todo.id === "id1")93 if (index !== -1) draft.splice(index, 1)94})9596// update by id97const updatedTodosArray = produce(todosArray, draft => {98 const index = draft.findIndex(todo => todo.id === "id1")99 if (index !== -1) draft[index].done = true100})101102// filtering items103const updatedTodosArray = produce(todosArray, draft => {104 // creating a new state is simpler in this example105 // (note that we don't need produce in this case,106 // but as shown below, if the filter is not on the top107 // level produce is still pretty useful)108 return draft.filter(todo => todo.done)docs: Update update-patterns.md to resetting/clearing (#1082)siddtheonebefore adoption17ac0b8
lines 109–113why, one step back109})110111// reset/clear/empty112const emptyTodo = produce(todosArray, () => {113 return [];docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 114–119why, one step back114})115```116117### Nested data structures118119```javascriptMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 120why, one step back120import {produce} from "immer"docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 121–148why, one step back121122// example complex data structure123const store = {124 users: new Map([125 [126 "17",127 {128 name: "Michel",129 todos: [130 {131 title: "Get coffee",132 done: false133 }134 ]135 }136 ]137 ])138}139140// updating something deeply in-an-object-in-an-array-in-a-map-in-an-object:141const nextStore = produce(store, draft => {142 draft.users.get("17").todos[0].done = true143})144145// filtering out all unfinished todo's146const nextStore = produce(store, draft => {147 const user = draft.users.get("17")148 // when filtering, creating a fresh collection is simpler thanchore: typo irrelvant => irrelevant (#866)Yanick Champouxbefore adoptionb6bf265
line 149why, one step back149 // removing irrelevant itemsdocs: unfinished todo filter condition (#1155)Ishrath Inaambefore adoption857b4b7
line 150why, one step back150 user.todos = user.todos.filter(todo => !todo.done)docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 151–156why, one step back151})152```153154Note 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)`.155156Note 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.