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

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>
主站蜘蛛池模板: 普定县| 花莲市| 抚宁县| 清水河县| 莱州市| 衡山县| 五台县| 如皋市| 东宁县| 黔东| 旌德县| 舟山市| 宜兴市| 金寨县| 建昌县| 昂仁县| 沐川县| 永昌县| 巴林左旗| 西昌市| 宣城市| 凤阳县| 东平县| 兴化市| 花莲县| 阳新县| 曲阳县| 福鼎市| 武威市| 新密市| 石狮市| 宜良县| 临沧市| 溧阳市| 牟定县| 德昌县| 乌拉特后旗| 宁陵县| 灵寿县| 延长县| 临江市|