diff --git a/beta/src/content/learn/rendering-lists.md b/beta/src/content/learn/rendering-lists.md index b7c4854b5b..584a18dd1b 100644 --- a/beta/src/content/learn/rendering-lists.md +++ b/beta/src/content/learn/rendering-lists.md @@ -1,74 +1,77 @@ --- -title: Rendering Lists +title: 渲染列表 +translator: + - Megrax + - rottenpen --- -You will often want to display multiple similar components from a collection of data. You can use the [JavaScript array methods](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#) to manipulate an array of data. On this page, you'll use [`filter()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) and [`map()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/map) with React to filter and transform your array of data into an array of components. +你可能经常需要通过 [JavaScript 的数组方法](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#) 来操作数组中的数据,从而将一个数据集渲染成多个相似的组件。在这篇文章中,你将学会如何在 React 中使用 [`filter()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) 筛选需要渲染的组件和使用 [`map()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/map) 把数组转换成组件数组。 -* How to render components from an array using JavaScript's `map()` -* How to render only specific components using JavaScript's `filter()` -* When and why to use React keys +* 如何通过 JavaScript 的 `map()` 方法从数组中生成组件 +* 如何通过 JavaScript 的 `filter()` 筛选需要渲染的组件 +* 何时以及为何使用 React 中的 key -## Rendering data from arrays {/*rendering-data-from-arrays*/} +## 从数组中渲染数据 {/*rendering-data-from-arrays*/} -Say that you have a list of content. +这里我们有一个列表。 ```js ``` -The only difference among those list items is their contents, their data. You will often need to show several instances of the same component using different data when building interfaces: from lists of comments to galleries of profile images. In these situations, you can store that data in JavaScript objects and arrays and use methods like [`map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) and [`filter()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) to render lists of components from them. +可以看到,这些列表项之间唯一的区别就是其中的内容/数据。未来你可能会碰到很多类似的情况,在那些场景中,你想基于不同的数据渲染出相似的组件,比如评论列表或者个人资料的图库。在这样的场景下,可以把要用到的数据存入 JavaScript 对象或数组,然后用 [`map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) 或 [`filter()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) 这样的方法来渲染出一个组件列表。 -Here’s a short example of how to generate a list of items from an array: +这里给出一个由数组生成一系列列表项的简单示例: -1. **Move** the data into an array: +1. 首先,把数据 **存储** 到数组中: ```js const people = [ - 'Creola Katherine Johnson: mathematician', - 'Mario José Molina-Pasquel Henríquez: chemist', - 'Mohammad Abdus Salam: physicist', - 'Percy Lavon Julian: chemist', - 'Subrahmanyan Chandrasekhar: astrophysicist' + '凯瑟琳·约翰逊: 数学家', + '马里奥·莫利纳: 化学家', + '穆罕默德·阿卜杜勒·萨拉姆: 物理学家', + '珀西·莱温·朱利亚: 化学家', + '苏布拉马尼扬·钱德拉塞卡: 天体物理学家', ]; ``` -2. **Map** the `people` members into a new array of JSX nodes, `listItems`: +2. **遍历** `people` 这个数组中的每一项,并获得一个新的 JSX 节点数组 `listItems`: ```js const listItems = people.map(person =>
  • {person}
  • ); ``` -3. **Return** `listItems` from your component wrapped in a `