官术网_书友最值得收藏!

Loops

A very common operation in UI development is to display lists of items. When it comes to showing lists, using JavaScript as a template language is a very good idea.

If we write a function that returns an array inside our JSX template, each element of the array gets compiled into an element.

As we have seen before, we can use any JavaScript expressions inside curly braces, and the most common way to generate an array of elements, given an array of objects, is to use map.

Let's dive into a real-world example. Suppose you have a list of users, each one with a name property attached to it.

To create an unordered list to show the users, you can do the following:

  <ul> 
{users.map(user => <li>{user.name}</li>)}
</ul>

This snippet is incredibly simple and incredibly powerful at the same time, where the power of the HTML and JavaScript converge.

主站蜘蛛池模板: 滦平县| 屯留县| 阳信县| SHOW| 永胜县| 井冈山市| 远安县| 开化县| 安平县| 平泉县| 桑植县| 中阳县| 凤冈县| 灵璧县| 阿克陶县| 清远市| 科技| 黎川县| 青铜峡市| 安陆市| 花莲市| 文安县| 陇西县| 方山县| 耿马| 金门县| 阿鲁科尔沁旗| 雷山县| 固原市| 柳江县| 霸州市| 白玉县| 临清市| 琼海市| 武城县| 萝北县| 溧阳市| 凭祥市| 子洲县| 新疆| 静海县|