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

Spaces

There's one thing that could be a little bit tricky in the beginning and, again, it concerns the fact that we should always keep in mind that JSX is not HTML, even if it has an XML-like syntax.

JSX handles the spaces between text and elements differently from HTML, in a way that's counter-intuitive.

Consider the following snippet:

  <div> 
<span>My</span>
name is
<span>Carlos</span>
</div>

In the browser, which interprets HTML, this code would give you "My name is Carlos", which is exactly what we expect.

In JSX, the same code would be rendered as MynameisCarlos, which is because the three nested lines get transpiled as individual children of the div element, without taking the spaces into account. A common solution to get the same output is putting a space explicitly between the elements as follows:

  <div> 
<span>My</span>
{' '}
name is
{' '}
<span>Carlos</span>
</div>

As you may have noticed, we are using an empty string wrapped inside a JavaScript expression to force the compiler to apply the space between the elements.

主站蜘蛛池模板: 新巴尔虎右旗| 长春市| 阿克陶县| 平顶山市| 九龙坡区| 大化| 信阳市| 夏津县| 梧州市| 酒泉市| 高平市| 海宁市| 湾仔区| 营山县| 沧州市| 曲靖市| 若尔盖县| 磴口县| 乌兰浩特市| 德清县| 连江县| 阿瓦提县| 攀枝花市| 株洲市| 瓮安县| 宜丰县| 高尔夫| 阳信县| 云安县| 连山| 英吉沙县| 抚州市| 德格县| 溆浦县| 大石桥市| 越西县| 天气| 儋州市| 南溪县| 施甸县| 米林县|