🎓 A1Learner
🔍
0 of 50 lessons completed

Rendering Lists

Rendering lists is turning an array into matching elements on the page. The map method builds one element for each item in the array.

One element per item

Map builds a list item for each fruit.

{fruits.map((fruit) => <li>{fruit}</li>)}

The array first

The array holds the three fruits.

const fruits = ['Apple', 'Banana', 'Cherry'];

The list on the page

The result is a list with three items.

<ul>
  {fruits.map((fruit) => <li>{fruit}</li>)}
</ul>

Example

export default function App() {
  const fruits = ['Apple', 'Banana', 'Cherry'];
  return (
    <ul>
      {fruits.map((fruit) => <li>{fruit}</li>)}
    </ul>
  );
}

Key points

  • An array can hold the items for a list.
  • The map method turns each item into one element.
  • Each item gets its own element in the result.
  • The list grows when the array grows.

In this example, the array holds three fruits. The map method builds one list item for each fruit. The page shows a list with three items. React prints a warning for this list in the console. The next lesson fixes that warning.

Note

React prints a warning in the console for this code.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which method turns each item in an array into an element?