Skip to Content
Node与Express开发(第2版)
book

Node与Express开发(第2版)

by Ethan Brown
June 2021
Intermediate to advanced
290 pages
9h 21m
Chinese
Posts & Telecom Press
Content preview from Node与Express开发(第2版)
174
|
第
16
章
JSX
中的花括号可以包含任何合法的
JavaScript
表达式,甚至可以包含其他
HTML
元素。
渲染列表就是一个常见的用例:
const colors = ['red', 'green', 'blue']
const html = (
<ul>
{colors.map(color =>
<li key={color}>{color}</li>
)}
</ul>
)
这个例子有几点需要注意。首先,注意我们把
colors
映射成了返回
<li>
元素。这点很关
键,因为
JSX
完
全依靠
表达式
运算。因此,
<ul>
必须要包含一个表达式或表达式的一个数
组。如果你把
map
改成了
forEach
,就会发现那些
<li>
元素没有被渲染。其次,注意那些
<li>
元素接收了一个属性
key
:这是性能上的一个让步。为了让
React
知道何时需要重新
渲染元素,对每个元素它都需要一个唯一键。既然我们的数组元素是唯一的,就可以直接
使用元素的值,但通常你会使用一个
ID
,或者
(如果没有其他属性可用的话)使用元素在
数组中的索引。
继续阅读之前,鼓励你体验一下
client/src/App.js
中的一些
JSX
例子
。如果一直运行
yarn
start
,每次保存修改时,修改的内容都会自动地在浏览器中反映出来,如此无疑会为你的
学习周期加速。
在结束本节内容之前,还有一个话题需要提及,那就是
状态
(
state
)
。每个组件都可以有自
己的状态。状态可以解释为“组件关联的可以修改的数据”。购物车就是一个很好的例子。
购物车组件可以包含商品项目的一个列表,当你增加或移除购物车中的商品时,这个组件 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

面向对象的思考过程(原书第5版)

面向对象的思考过程(原书第5版)

马特 魏斯费尔德
精通模块化JavaScript

精通模块化JavaScript

Nicolás Bevacqua

Publisher Resources

ISBN: 9787115565099