update-patterns.md
This file
Path
website/i18n/zh-CN/docusaurus-plugin-content-docs/current/update-patterns.md
142 lines · 3.3 KiB · Markdown
Last changed by
Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
History of this fileTake it
The walk stopped after 200 commits: lines older than 07f5cc9 point at it.
chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 1–16why, one step back1---2id: update-patterns3title: 更新模式4---56<center>7<div data-ea-publisher="immerjs" data-ea-type="image" class="horizontal bordered"></div>8</center>910在 Immer 之前,使用不可变数据意味着学习所有不可变的更新模式。1112为了帮助“忘记”这些模式,这里概述了如何利用内置 JavaScript API 来更新对象和集合1314### 更新对象1516```javascriptMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 17why, one step back17import {produce} from "immer"chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
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// 添加25const addedTodosObj = produce(todosObj, draft => {26 draft["id3"] = {done: false, body: "Buy bananas"}27})2829// 删除30const deletedTodosObj = produce(todosObj, draft => {31 delete draft["id1"]32})3334// 更新35const updatedTodosObj = produce(todosObj, draft => {36 draft["id1"].done = true37})38```3940### 更新数组4142```javascriptMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 43why, one step back43import {produce} from "immer"chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 44–106why, one step back4445const todosArray = [46 {id: "id1", done: false, body: "Take out the trash"},47 {id: "id2", done: false, body: "Check Email"}48]4950// 添加51const addedTodosArray = produce(todosArray, draft => {52 draft.push({id: "id3", done: false, body: "Buy bananas"})53})5455// 索引删除56const deletedTodosArray = produce(todosArray, draft => {57 draft.splice(3 /*索引 */, 1)58})5960// 索引更新61const updatedTodosArray = produce(todosArray, draft => {62 draft[3].done = true63})6465// 索引插入66const updatedTodosArray = produce(todosArray, draft => {67 draft.splice(3, 0, {id: "id3", done: false, body: "Buy bananas"})68})6970// 删除最后一个元素71const updatedTodosArray = produce(todosArray, draft => {72 draft.pop()73})7475// 删除第一个元素76const updatedTodosArray = produce(todosArray, draft => {77 draft.shift()78})7980// 数组开头添加元素81const addedTodosArray = produce(todosArray, draft => {82 draft.unshift({id: "id3", done: false, body: "Buy bananas"})83})8485// 根据 id 删除86const deletedTodosArray = produce(todosArray, draft => {87 const index = draft.findIndex(todo => todo.id === "id1")88 if (index !== -1) draft.splice(index, 1)89})9091// 根据 id 更新92const updatedTodosArray = produce(todosArray, draft => {93 const index = draft.findIndex(todo => todo.id === "id1")94 if (index !== -1) draft[index].done = true95})9697// 过滤98const updatedTodosArray = produce(todosArray, draft => {99 // 过滤器实际上会返回一个不可变的状态,但是如果过滤器不是处于对象的顶层,这个依然很有用100 return draft.filter(todo => todo.done)101})102```103104### 嵌套数据结构105106```javascriptMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 107why, one step back107import {produce} from "immer"chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 108–142why, one step back108109// 复杂数据结构例子110const store = {111 users: new Map([112 [113 "17",114 {115 name: "Michel",116 todos: [117 {118 title: "Get coffee",119 done: false120 }121 ]122 }123 ]124 ])125}126127// 深度更新128const nextStore = produce(store, draft => {129 draft.users.get("17").todos[0].done = true130})131132// 过滤133const nextStore = produce(store, draft => {134 const user = draft.users.get("17")135136 user.todos = user.todos.filter(todo => todo.done)137})138```139140请注意,许多数组操作可用于通过传递多个参数或使用展开操作来一次插入多个元素:`todos.unshift(...items)`。141142请注意,当处理包含通常由某个 id 标识的对象的数组时,我们建议使用基于 `Map` 或索引的对象(如上所示)而不是执行频繁的查找操作,查找表通常执行效率更高。