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

Children

JSX allows you to define children to describe the tree of elements and compose complex UIs.

A basic example is a link with text inside it, as follows:

  <a href="https://dev.education">Click me!</a>

This would be transpiled into the following:

  React.createElement( 
"a",
{ href: "https://www.dev.education" },
"Click me!"
);

Our link can be enclosed inside a div element for some layout requirements, and the JSX snippet to achieve that is as follows:

  <div> 
<a href="https://www.dev.education">Click me!</a>
</div>

The JavaScript equivalent is as follows:

  React.createElement( 
"div",
null,
React.createElement(
"a",
{ href: "https://www.dev.education" },
"Click me!"
)
);

It should now be clear how the XML-like syntax of JSX makes everything more readable and maintainable, but it is always important to know the JavaScript parallel of our JSX to have control over the creation of elements.

The good part is that we are not limited to having elements as children of elements, but we can use JavaScript expressions, such as functions or variables.

To do this, we have to enclose the expression within curly braces:

  <div> 
Hello, {variable}.
I'm a {() => console.log('Function')}.
</div>

The same applies to non-string attributes, as follows:

  <a href={this.createLink()}>Click me!</a>
主站蜘蛛池模板: 舒城县| 宁乡县| 财经| 景宁| 汝阳县| 揭东县| 文化| 淮滨县| 寻乌县| 克拉玛依市| 新晃| 额尔古纳市| 民勤县| 广饶县| 仪陇县| 肃南| 曲阜市| 从江县| 靖远县| 甘南县| 利辛县| 大理市| 特克斯县| 全南县| 江口县| 霍城县| 固原市| 鲜城| 门源| 千阳县| 呈贡县| 遂平县| 芦溪县| 安义县| 德兴市| 湖口县| 汤阴县| 二连浩特市| 景泰县| 上高县| 乐都县|