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

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.

主站蜘蛛池模板: 高唐县| 桦南县| 美姑县| 龙陵县| 汉阴县| 泸西县| 神农架林区| 藁城市| 崇文区| 类乌齐县| 澎湖县| 新河县| 朝阳市| 呼伦贝尔市| 凤山市| 南投市| 台山市| 安化县| 伊通| 隆回县| 玉林市| 江达县| 永兴县| 邵阳市| 通州区| 利川市| 房山区| 太白县| 佛学| 文安县| 交口县| 桂东县| 石家庄市| 麻城市| 洛隆县| 邓州市| 仁化县| 岢岚县| 浦北县| 西和县| 左权县|