添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Learn React

Rendering Lists

You will often want to display multiple similar components from a collection of data. You can use the JavaScript array methods to manipulate an array of data. On this page, you’ll use filter() and map() with React to filter and transform your array of data into an array of components.

You will learn

  • 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
  • Rendering data from arrays

    Say that you have a list of content.

    <ul>
    <li>Creola Katherine Johnson: mathematician</li>
    <li>Mario José Molina-Pasquel Henríquez: chemist</li>
    <li>Mohammad Abdus Salam: physicist</li>
    <li>Percy Lavon Julian: chemist</li>
    <li>Subrahmanyan Chandrasekhar: astrophysicist</li>
    </ul>

    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() and filter() to render lists of components from them.

    Here’s a short example of how to generate a list of items from an array:

  • Move the data into an array:
  • const people = [
    'Creola Katherine Johnson: mathematician',
    'Mario José Molina-Pasquel Henríquez: chemist',
    'Mohammad Abdus Salam: physicist',
    'Percy Lavon Julian: chemist',
    'Subrahmanyan Chandrasekhar: astrophysicist'
    ];
  • Map the people members into a new array of JSX nodes, listItems :
  • const listItems = people.map(person => <li>{person}</li>);
  • Return listItems from your component wrapped in a <ul> :
  • return <ul>{listItems}</ul>;

    Here is the result:

const people = [
  'Creola Katherine Johnson: mathematician',
  'Mario José Molina-Pasquel Henríquez: chemist',
  'Mohammad Abdus Salam: physicist',
  'Percy Lavon Julian: chemist',
  'Subrahmanyan Chandrasekhar: astrophysicist'
export default function List() {
  const listItems = people.map(person =>
    <li>{person}</li>
  return <ul>{listItems}</ul>;

Notice the sandbox above displays a console error:

Console
Warning: Each child in a list should have a unique “key” prop.

You’ll learn how to fix this error later on this page. Before we get to that, let’s add some structure to your data.

Filtering arrays of items

This data can be structured even more.

const people = [{
id: 0,
name: 'Creola Katherine Johnson',
profession: 'mathematician',
}, {
id: 1,
name: 'Mario José Molina-Pasquel Henríquez',
profession: 'chemist',
}, {
id: 2,
name: 'Mohammad Abdus Salam',
profession: 'physicist',
}, {
id: 3,
name: 'Percy Lavon Julian',
profession: 'chemist',
}, {
id: 4,
name: 'Subrahmanyan Chandrasekhar',
profession: 'astrophysicist',
}];

Let’s say you want a way to only show people whose profession is 'chemist'. You can use JavaScript’s filter() method to return just those people. This method takes an array of items, passes them through a “test” (a function that returns true or false), and returns a new array of only those items that passed the test (returned true).

You only want the items where profession is 'chemist'. The “test” function for this looks like (person) => person.profession === 'chemist'. Here’s how to put it together:

  • Create a new array of just “chemist” people, chemists, by calling filter() on the people filtering by person.profession === 'chemist':
  • const chemists = people.filter(person =>
    person.profession === 'chemist'
    );
  • Now map over chemists:
  • const listItems = chemists.map(person =>
    <li>
    <img
    src={getImageUrl(person)}
    alt={person.name}
    />
    <p>
    <b>{person.name}:</b>
    {' ' + person.profession + ' '}
    known for {person.accomplishment}
    </p>
    </li>
    );
  • Lastly, return the listItems from your component:
  • return <ul>{listItems}</ul>;
    import { people } from './data.js';
    import { getImageUrl } from './utils.js';
    export default function List() {
      const chemists = people.filter(person =>
        person.profession === 'chemist'
      const listItems = chemists.map(person =>
            src={getImageUrl(person)}
            alt={person.name}
            <b>{person.name}:</b>
            {' ' + person.profession + ' '}
            known for {person.accomplishment}
      return <ul>{listItems}</ul>;