Skip to content
Myr

Took improvements from #426 and #115, added patterns page to the docs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by Michel Weststrate · parent 35b80db · Browse files · 4 files+122 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+122 added−4 removed

  • other:+122 added−4 removed

4 files+122 added−4 removed

modified:docs/example-reducer.md+3 added-3 removed
added:docs/update-patterns.md+115 added
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:1112```javascript13import produce from "immer"1415// object mutations16const todosObj = {17 id1: {done: false, body: "Take out the trash"},18 id2: {done: false, body: "Check Email"}19}2021// add22const addedTodosObj = produce(todosObj, draft => {23 draft["id3"] = {done: false, body: "Buy bananas"}24})2526// delete27const deletedTodosObj = produce(todosObj, draft => {28 delete draft["id1"]29})3031// update32const updatedTodosObj = produce(todosObj, draft => {33 draft["id1"].done = true34})3536// array mutations37const todosArray = [38 {id: "id1", done: false, body: "Take out the trash"},39 {id: "id2", done: false, body: "Check Email"}40]4142// add43const addedTodosArray = produce(todosArray, draft => {44 draft.push({id: "id3", done: false, body: "Buy bananas"})45})4647// delete by index48const deletedTodosArray = produce(todosArray, draft => {49 draft.splice(3 /*the index */, 1)50})5152// update by index53const updatedTodosArray = produce(todosArray, draft => {54 draft[3].done = true55})5657// insert at index58const updatedTodosArray = produce(todosArray, draft => {59 draft.splice(3, 0, {id: "id3", done: false, body: "Buy bananas"})60})6162// remove last item63const updatedTodosArray = produce(todosArray, draft => {64 draft.pop()65})6667// remove first item68const updatedTodosArray = produce(todosArray, draft => {69 draft.shift()70})7172// add item at the beginning of the array73const addedTodosArray = produce(todosArray, draft => {74 draft.unshift({id: "id3", done: false, body: "Buy bananas"})75})7677// delete by id78const deletedTodosArray = produce(todosArray, draft => {79 const index = draft.findIndex(todo => todo.id === "id1")80 if (index !== -1) draft.splice(index, 1)81})8283// update by id84const updatedTodosArray = produce(todosArray, draft => {85 const index = draft.findIndex(todo => todo.id === "id1")86 if (index !== -1) draft[index].done = true87})8889// example complex data structure90const store = {91 users: new Map([92 [93 "17",94 {95 name: "Michel",96 todos: [97 {98 title: "Get coffee",99 done: false100 }101 ]102 }103 ]104 ])105}106107// updating something deeply in-an-object-in-an-array-in-a-map-in-an-object:108const nextStore = produce(store, draft => {109 draft.users.get("17").todos[0].done = true110})111```112113Note 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)`.114115Note 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.
modified:website/i18n/en.json+3 added
modified:website/sidebars.json+1 added-1 removed