- React Design Patterns and Best Practices(Second Edition)
- Carlos Santana Roldán
- 188字
- 2021-06-24 15:43:37
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>
- 物聯網標準化指南
- CorelDRAW X6 中文版圖形設計實戰從入門到精通
- EDA技術與VHDL編程
- 異構基因共表達網絡的分析方法
- 物聯網檢驗檢測技術
- 城市治理一網統管
- Building RESTful Web services with Go
- Microsoft Dynamics CRM 2011 Applications(MB2-868) Certification Guide
- 6G新技術 新網絡 新通信
- Working with Legacy Systems
- Master Apache JMeter:From Load Testing to DevOps
- TD-LTE無線網絡規劃與設計
- Microsoft Power Platform Enterprise Architecture
- 商業的本質和互聯網
- 想象的互動:網絡人際傳播中的印象形成