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

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.

主站蜘蛛池模板: 平潭县| 金秀| 苏尼特右旗| 西宁市| 阆中市| 宜都市| 马边| 肥城市| 扶风县| 邓州市| 新巴尔虎右旗| 周至县| 张北县| 监利县| 西林县| 秦安县| 汪清县| 瓦房店市| 齐河县| 沅陵县| 喜德县| 珠海市| 乐安县| 阳东县| 乡宁县| 富锦市| 仙居县| 镇沅| 五家渠市| 文昌市| 长武县| 伊川县| 吴江市| 历史| 神农架林区| 甘孜县| 太和县| 漾濞| 九龙城区| 双桥区| 建湖县|