- 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>
- 物聯(lián)網(wǎng)標(biāo)準(zhǔn)化指南
- 黑客攻防實(shí)戰(zhàn)技術(shù)完全手冊:掃描、嗅探、入侵與防御
- Hands-On Chatbots and Conversational UI Development
- Oracle SOA Suite 11g Performance Tuning Cookbook
- PLC、現(xiàn)場總線及工業(yè)網(wǎng)絡(luò)實(shí)用技術(shù)速成
- 2018網(wǎng)信發(fā)展報(bào)告
- 數(shù)字調(diào)制解調(diào)技術(shù)的MATLAB與FPGA實(shí)現(xiàn):Altera/Verilog版(第2版)
- C/C++串口通信:典型應(yīng)用實(shí)例編程實(shí)踐
- Kong網(wǎng)關(guān):入門、實(shí)戰(zhàn)與進(jìn)階
- VMware NSX網(wǎng)絡(luò)虛擬化入門
- 從實(shí)踐中學(xué)習(xí)手機(jī)抓包與數(shù)據(jù)分析
- 圖神經(jīng)網(wǎng)絡(luò)前沿
- 移動(dòng)互聯(lián)網(wǎng)新思維
- 物聯(lián)網(wǎng)
- RestKit for iOS