From 702b1b281d1a44f2a7e45f67017aa41d5f5f00db Mon Sep 17 00:00:00 2001 From: zhuye Date: Sat, 6 Nov 2021 11:51:48 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E5=AE=8C=E6=88=901-400=E8=A1=8C=E7=BF=BB?= =?UTF-8?q?=E8=AF=91=E5=88=9D=E7=A8=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pages/learn/updating-arrays-in-state.md | 104 +++++++++--------- 1 file changed, 53 insertions(+), 51 deletions(-) diff --git a/beta/src/pages/learn/updating-arrays-in-state.md b/beta/src/pages/learn/updating-arrays-in-state.md index 429a17d81e..316b525b7c 100644 --- a/beta/src/pages/learn/updating-arrays-in-state.md +++ b/beta/src/pages/learn/updating-arrays-in-state.md @@ -1,52 +1,52 @@ --- -title: Updating Arrays in State +title: 修改 State 中的数组 --- -Arrays are another type of mutable JavaScript objects you can store in state and should treat as immutable. Just like with objects, when you want to update an array stored in state, you need to create a new one (or make a copy of an existing one), and then set state to use the new array. +数组是另外一种可以存储在 state 中的 JavaScript 对象,它虽然是可变的,但是却应该被视为不可变的。同对象一样,当你想要更新 state 中的数组的时候,你需要创建一个新的数组(或者将已有的数组复制成一个新的数组),并将这个新数组设置到 state 上。 -- How to add, remove, or change items in an array in React state -- How to update an object inside of an array -- How to make array copying less repetitive with Immer +- 如何添加、删除或者修改 React state 中的数组中某个元素的值 +- 如何更新数组中某个类型为对象的元素 +- 如何使用 Immer 来降低数组重复复制的次数 -## Updating arrays without mutation +## 不使用 mutation 的情况下更新数组 -In JavaScript, arrays are just another kind of object. [Like with objects](/learn/updating-objects-in-state), **you should treat arrays in React state as read-only**. This means that you shouldn't reassign items inside an array like `arr[0] = 'bird'`, and you also shouldn't use methods that mutate the array, such as `push()` and `pop()`. +在 JavaScript 中,数组就是对象的一种. [同对象一样](/learn/updating-objects-in-state), **你需要将 React state 中的数组视为只读的**。这意味着你不应该使用类似于 `arr[0] = 'bird'` 这样的方式来修改数组中的某个元素,你也不应该使用会改变原始数组的方法,例如 `push()` 和 `pop()`。 -Instead, every time you want to update an array, you'll want to pass a *new* array to your state setting function. To do that, you can create a new array from the original array in your state by calling its non-mutating methods like `filter()` and `map()`. Then you can set your state to the resulting new array. +取而代之的,每次你想要更新一个数组时,你需要把一个*新*的数组传入 state 的 setting 方法中。为了实现这一点,你可以通过使用一些不会改变原始数组的方法,例如 `filter()` 和 `map()` ,从原始数组创建一个新的数组。之后你就可以把这个新创建的数组设置到 state 上。 -Here is a reference table of common array operations. When dealing with arrays inside React state, you will need to avoid the methods in the left column, and instead prefer the methods in the right column: +这里是数组相关操作的一张参考表。当你对 React state 中的数组进行操作时,你需要避免使用表格左侧的方法,而尽量使用表格右侧的方法: -| | avoid (mutates the array) | prefer (returns a new array) | +| | 避免使用 (会改变原始数组) | 推荐使用 (会返回一个新数组) | |---------|----------------|-------------------| -| adding | `push`, `unshift` | `concat`, `[...arr]` spread syntax ([example](#adding-to-an-array))| -| removing | `pop`, `shift`, `splice` | `filter`, `slice` ([example](#removing-from-an-array)) -|replacing| `splice`, `arr[i] = ...` assignment | `map` ([example](#replacing-items-in-an-array)) | -|sorting| `reverse`, `sort` | copy the array first ([example](#making-other-changes-to-an-array)) | +| 添加元素 | `push` , `unshift` | `concat` , `[...arr]` 展开语法 ([例子](#向数组中添加元素))| +| 删除元素 | `pop` , `shift` , `splice` | `filter` , `slice` ([例子](#从数组中删除元素)) +| 替换元素 | `splice` , `arr[i] = ...` 赋值 | `map` ([例子](#替换数组中的元素)) | +| 排序 | `reverse` , `sort` | 先将数组复制一份 ([例子](#其他改变数组的情况)) | -Alternatively, you can [use Immer](#write-concise-update-logic-with-immer) which lets you use methods from both columns. +或者,你可以[使用 Immer](#使用-Immer-编写简洁的更新逻辑) ,这样你便可以使用表格中的所有方法了。 -Unfortunately, [`slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) and [`splice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) are named similarly but are very different: +不幸的是,[`slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) 和 [`splice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) 虽然名字相似,但是却迥然不同: -* `slice` lets you copy an array or a part of it. -* `splice` **mutates** the array (to insert or delete items). +* `slice` 使得你可以复制数组或者它的子数组。 +* `splice` **会改变** 原始数组(插入或者删除元素)。 -In React, you will be using `slice` (no `p`!) a lot more often because you don't want to mutate objects or arrays in state. [Updating Objects](/learn/updating-objects-in-state) explains what mutation is and why it's not recommended for state. +在 React 中,更多情况下你会使用 `slice` (没有 `p` !),因为你不想改变 state 中的对象或数组。[更新对象](/learn/updating-objects-in-state)这一章节解释了什么是 mutation 以及为什么不推荐在 state 里使用它。 -### Adding to an array +### 向数组中添加元素 -`push()` will mutate an array, which you don't want: +`push()` 会改变原始数组,你应该避免使用它: @@ -61,7 +61,7 @@ export default function List() { return ( <> -

Inspiring sculptors:

+

令人惊叹的雕塑家们:

setName(e.target.value)} @@ -72,7 +72,7 @@ export default function List() { id: nextId++, name: name, }); - }}>Add + }}>添加