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

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.

主站蜘蛛池模板: 岐山县| 同心县| 龙胜| 剑河县| 河北区| 怀柔区| 四子王旗| 蛟河市| 永济市| 锡林浩特市| 垦利县| 大安市| 石家庄市| 托克逊县| 棋牌| 务川| 菏泽市| 东光县| 偃师市| 屏边| 体育| 内江市| 顺平县| 南宁市| 大宁县| 临沂市| 城固县| 铜陵市| 宜章县| 弥渡县| 通州区| 宁城县| 滁州市| 交口县| 赞皇县| 洞头县| 大埔县| 甘洛县| 平定县| 且末县| 静宁县|