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

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.

主站蜘蛛池模板: 荔浦县| 宣威市| 沐川县| 安国市| 林甸县| 文安县| 文安县| 南澳县| 鱼台县| 东山县| 包头市| 修文县| 上饶县| 方城县| 洛浦县| 云霄县| 新邵县| 阿尔山市| 基隆市| 广州市| 施甸县| 平山县| 安平县| 黄冈市| 蓝田县| 福安市| 重庆市| 兰考县| 淳化县| 梨树县| 江都市| 巩义市| 定南县| 湄潭县| 恭城| 江山市| 浦江县| 鄂托克旗| 兴和县| 建宁县| 平阳县|