Skip to content

Commit 4a0576b

Browse files
committed
docs: jsx
1 parent e21783a commit 4a0576b

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

docs/.vitepress/config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ export default defineConfig({
5454
link: '/components/lifecycle',
5555
},],
5656
},
57+
{
58+
text: 'Describing the UI', items: [{
59+
text: 'JSX',
60+
link: '/jsx/overview',
61+
}, {
62+
text: 'Writing JSX',
63+
link: '/jsx/writing-jsx',
64+
}]
65+
}
5766
]
5867
},
5968

docs/jsx/overview.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Overview
2+
3+
JSX is a syntax extension to JavaScript. It was developed by Facebook to improve the development experience of writing JavaScript code for React applications. It is similar to a template language, but it has full power of JavaScript.
4+
5+
::: warning
6+
You might think that HTML is very similar to JSX, but the key difference is that JSX has full power of JavaScript. Also, JSX syntax is different from HTML syntax, so you can't just drop JSX into a HTML file and expect it to work.
7+
8+
You can see how to convert HTML to JSX in the [Converting HTML to JSX](./writing-jsx.md#converting-html-to-jsx) section of the [Writing JSX](./writing-jsx.md) page. It also describes how to convert JSX back to HTML.
9+
:::
10+
11+
JSX is not a requirement for using React, but it is a common way to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript.
12+
13+
## JSX Demo
14+
15+
JSX is not a separate programming language, but rather a syntax extension for JavaScript. This means that you can use JSX within your JavaScript code, just like you would use any other JavaScript expression.
16+
17+
JSX uses angle brackets (`< >`) to enclose HTML-like tags, which can contain attributes and children. For example, you can use JSX to create a simple component like this:
18+
19+
```js
20+
const element = <h1>Hello, world!</h1>;
21+
```
22+
23+
JSX tags may contain children:
24+
25+
```js
26+
const element = (
27+
<h1 className="greeting">
28+
Hello, world!
29+
</h1>
30+
);
31+
```
32+
33+
JSX tags may contain expressions:
34+
35+
```js
36+
const name = 'Josh Perez';
37+
const element = <h1>Hello, {name}</h1>;
38+
```

docs/jsx/writing-jsx.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Writing JSX
2+
3+
## Converting HTML to JSX
4+
5+
Here is an example of how you might convert a simple HTML file to JSX:
6+
7+
```html
8+
<div class="container">
9+
<h1>Hello World!</h1>
10+
<h2>Good to see you here.</h2>
11+
<img src="https://example.com/favicon.ico" alt="Logo" >
12+
</div>
13+
```
14+
15+
If you put it into JSX, it would create a error. JSX tags must have a closing tag, and the `<img>` tag doesn't have a closing tag. So you need to add a closing tag to the `<img>` tag.
16+
17+
So the JSX version of the above HTML would be:
18+
19+
```js
20+
<div className="container">
21+
<h1>Hello World!</h1>
22+
<h2>Good to see you here.</h2>
23+
<img src="https://example.com/favicon.ico" alt="Logo" /> // [!code focus]
24+
</div>
25+
```
26+
27+
## Rules of JSX
28+
29+
### Must return a single element
30+
31+
JSX must have a single root element. If you want to return multiple elements, you need to wrap them in a single element.
32+
33+
Here is a wrong example:
34+
35+
```js
36+
const element = (
37+
<h1>Hello!</h1>
38+
<h2>Good to see you here.</h2>
39+
);
40+
```
41+
42+
Here is a correct example:
43+
44+
```js
45+
const element = (
46+
<div>
47+
<h1>Hello!</h1>
48+
<h2>Good to see you here.</h2>
49+
</div>
50+
);
51+
```
52+
53+
### Don't want to add a wrapper element?
54+
55+
If you don't want to add a wrapper element, you can use Fragment to wrap multiple elements.
56+
57+
```js
58+
const element = (
59+
<>
60+
<h1>Hello!</h1>
61+
<h2>Good to see you here.</h2>
62+
</>
63+
);
64+
```
65+
66+
`<>` is a shorthand for `Fragment`. It is a common pattern to use Fragment to wrap multiple elements. It is also a good practice to use Fragment when you don't want to add a wrapper element.
67+
68+
```js
69+
const element = (
70+
<Fragment>
71+
<h1>Hello!</h1>
72+
<h2>Good to see you here.</h2>
73+
</Fragment>
74+
);
75+
```
76+
77+
### Must be closed
78+
79+
JSX tags must be closed. If you don't want to add a child element, you can use self-closing tag.
80+
81+
```js
82+
<div>
83+
<h1>Hello!</h1>
84+
<br /> // [!code focus]
85+
<h2>Good to see you here.</h2>
86+
<hr /> // [!code focus]
87+
<img src="https://example.com/favicon.ico" alt="Logo" /> // [!code focus]
88+
</div>
89+
```
90+
91+
### camelCase all HTML attributes (except `class` )
92+
93+
JSX in jwc.js is different from usual JSX. We have changed some attributes to the original attribute name.
94+
95+
::: warning
96+
Because jwc.js still in `alpha` status, we may change the attribute name in the future.
97+
:::
98+
99+
1. camelCase all most of the **event** names. For example, `onclick` becomes `onClick`.
100+
101+
```js
102+
<button onClick={handleClick}>Click me</button>
103+
```
104+
105+
2. `class` remains the same <Badge text="Pending" type="warning" />. It's different from React, which uses `className`.
106+
107+
```js
108+
<div class="container"></div>
109+
```

0 commit comments

Comments
 (0)