- React Design Patterns and Best Practices(Second Edition)
- Carlos Santana Roldán
- 177字
- 2021-06-24 15:43:38
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.
- RCNP實(shí)驗(yàn)指南:構(gòu)建高級的路由互聯(lián)網(wǎng)絡(luò)(BARI)
- Web安全防護(hù)指南:基礎(chǔ)篇
- 物聯(lián)網(wǎng)安全(原書第2版)
- 網(wǎng)絡(luò)互聯(lián)技術(shù)(實(shí)踐篇)
- 2018網(wǎng)信發(fā)展報(bào)告
- 區(qū)塊鏈輕松上手:原理、源碼、搭建與應(yīng)用
- Working with Legacy Systems
- 局域網(wǎng)組成實(shí)踐
- TCP/IP基礎(chǔ)(第2版)
- 云工廠:開啟中國制造云時(shí)代
- Web用戶查詢?nèi)罩就诰蚺c應(yīng)用
- 人際網(wǎng)絡(luò)
- 小型局域網(wǎng)組建
- Hands-On Microservices:Monitoring and Testing
- 物聯(lián)網(wǎng):感知、傳輸與應(yīng)用