diff --git a/beta/src/content/learn/updating-arrays-in-state.md b/beta/src/content/learn/updating-arrays-in-state.md index eae89facf3..57d6f64f67 100644 --- a/beta/src/content/learn/updating-arrays-in-state.md +++ b/beta/src/content/learn/updating-arrays-in-state.md @@ -1,52 +1,56 @@ --- -title: Updating Arrays in State +title: 更新 state 中的数组 +translators: + - yliaz + - takeItIzzy + - KnowsCount --- -Arrays are mutable in JavaScript, but you should treat them as immutable when you store them in state. 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 {/*updating-arrays-without-mutation*/} +## 在没有 mutation 的前提下更新数组 {/*updating-arrays-without-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]` 展开语法([例子](#adding-to-an-array))| +| 删除元素 | `pop`,`shift`,`splice` | `filter`,`slice`([例子](#removing-from-an-array)) +| 替换元素 | `splice`,`arr[i] = ...` 赋值 | `map`([例子](#replacing-items-in-an-array)) | +| 排序 | `reverse`,`sort` | 先将数组复制一份([例子](#making-other-changes-to-an-array)) | -Alternatively, you can [use Immer](#write-concise-update-logic-with-immer) which lets you use methods from both columns. +或者,你可以[使用 Immer](#write-concise-update-logic-with-immer) ,这样你便可以使用表格中的所有方法了。 -Unfortunately, [`slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) 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/slice) 和 [`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 {/*adding-to-an-array*/} +### 向数组中添加元素 {/*adding-to-an-array*/} -`push()` will mutate an array, which you don't want: +`push()` 会直接修改原始数组,而你不希望这样: @@ -61,7 +65,7 @@ export default function List() { return ( <> -

Inspiring sculptors:

+

振奋人心的雕塑家们:

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