Skip to content
This repository was archived by the owner on Mar 1, 2025. It is now read-only.

Commit 281cbbb

Browse files
Merge pull request #13 from xszhangxiaocuo/main
day3:JSX
2 parents 6f35ae0 + ba4b079 commit 281cbbb

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Hoshino_FullStack_Compass.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,40 @@
2121

2222
还有一个css的布局属性`display:block`会使一个元素自动占据一行, 导致本该渲染在同一行的元素呈`column`布局,使用`display:inline`布局可以解决这个问题。
2323

24+
### 02.15
2425

26+
JSX 是一个看起来很像 XML 的 JavaScript 语法扩展。
27+
28+
- JSX 执行更快,因为它在编译为 JavaScript 代码后进行了优化。
29+
- 它是类型安全的,在编译过程中就能发现错误。
30+
- 使用 JSX 编写模板更加简单快速。
31+
32+
React 认为渲染逻辑本质上与其他 UI 逻辑内在耦合,比如,在**UI**中需要绑定处理事件、在某些时刻状态发生变化时需要通知到**UI**,以及需要在**UI**中展示准备好的数据。但是React不强制要求使用JSX。
33+
34+
JSX是JS的语法糖,编译时JSX会通过Babel编译成JS。React源码中使用React.createElement()方法来创建JSX,该方法依次接收DOM节点(比如div、span)、属性集合(比如className、style)和children(子节点)三个参数,并返回一个JS对象,也就是虚拟DOM。
35+
36+
以下两段示例是等价的:
37+
38+
```jsx
39+
const element = (
40+
<h1 className="greeting">
41+
Hello, world!
42+
</h1>
43+
);
44+
```
45+
46+
```jsx
47+
const element = React.createElement(
48+
'h1',
49+
{className: 'greeting'},
50+
'Hello, world!'
51+
);
52+
```
53+
54+
要从组件返回多个元素,必须用一个`parent tag`包裹它们,可以使用`<div></div>`或者一个空标签`<></>`
55+
56+
JSX 要求标签明确关闭:像 `<img>` 这样的自闭合标签必须写成 `<img />`,像 `<li>oranges` 这样的包装标签必须写成 `<li>oranges</li>`
57+
58+
可以使用[转换器](https://transform.tools/html-to-jsx),将现有的`html`代码转换为`JSX`代码。
2559

2660
<!-- Content_END -->

0 commit comments

Comments
 (0)