Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Beta] docs(cn): add writing-markup-with-jsx #673

Merged
merged 21 commits into from
May 27, 2022
Merged
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b58736c
[Beta] docs(cn): add writing-markup-with-jsx
songhn233 Nov 24, 2021
c8f84c9
Update beta/src/pages/learn/writing-markup-with-jsx.md
songhn233 Dec 6, 2021
4cc0e10
Update beta/src/pages/learn/writing-markup-with-jsx.md
songhn233 Dec 6, 2021
83f9207
Update beta/src/pages/learn/writing-markup-with-jsx.md
songhn233 Dec 6, 2021
88a0f0c
Update beta/src/pages/learn/writing-markup-with-jsx.md
songhn233 Dec 6, 2021
06b685a
Update beta/src/pages/learn/writing-markup-with-jsx.md
songhn233 Dec 6, 2021
5479381
Update beta/src/pages/learn/writing-markup-with-jsx.md
songhn233 Dec 6, 2021
2905015
Update beta/src/pages/learn/writing-markup-with-jsx.md
songhn233 Dec 6, 2021
8337bc3
Update beta/src/pages/learn/writing-markup-with-jsx.md
songhn233 Dec 6, 2021
e44657b
Update beta/src/pages/learn/writing-markup-with-jsx.md
songhn233 Dec 6, 2021
c3e23be
Update beta/src/pages/learn/writing-markup-with-jsx.md
songhn233 Dec 6, 2021
5c8a04c
Update beta/src/pages/learn/writing-markup-with-jsx.md
songhn233 Dec 6, 2021
2e01044
Update beta/src/pages/learn/writing-markup-with-jsx.md
songhn233 Dec 6, 2021
542053c
Update writing-markup-with-jsx.md
songhn233 Dec 7, 2021
66d22e6
Update beta/src/pages/learn/writing-markup-with-jsx.md
songhn233 Feb 8, 2022
a558b62
Update beta/src/pages/learn/writing-markup-with-jsx.md
songhn233 Feb 8, 2022
fbdd32c
Update beta/src/pages/learn/writing-markup-with-jsx.md
songhn233 Feb 8, 2022
efab232
Merge branch 'main' into beta-writing-with-jsx
QC-L May 27, 2022
2d06840
Update beta/src/pages/learn/writing-markup-with-jsx.md
QC-L May 27, 2022
9e73dd1
Update beta/src/pages/learn/writing-markup-with-jsx.md
QC-L May 27, 2022
fb17905
Apply suggestions from code review
QC-L May 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 69 additions & 69 deletions beta/src/pages/learn/writing-markup-with-jsx.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
---
title: Writing Markup with JSX
title: 使用 JSX 书写标记语言
---
QC-L marked this conversation as resolved.
Show resolved Hide resolved

<Intro>

JSX is a syntax extension for JavaScript that lets you write HTML-like markup inside a JavaScript file. Although there are other ways to write components, most React developers prefer the conciseness of JSX, and most codebases use it.
QC-L marked this conversation as resolved.
Show resolved Hide resolved
JSX 是一个 JavaScript 语法扩展,可以让你在 JavaScript 文件中书写类似 HTML 的标记。虽然还有其它方式可以编写组件,但大部分 React 开发者更喜欢 JSX 的简洁性,并且在大部分代码库中使用它。

</Intro>

<YouWillLearn>

* Why React mixes markup with rendering logic
* How JSX is different from HTML
* How to display information with JSX
* 为什么 React 将标记和渲染逻辑耦合在一起
* JSX HTML 有什么区别
* 如何通过 JSX 展示信息

</YouWillLearn>

## JSX: Putting markup into JavaScript {/*jsx-putting-markup-into-javascript*/}
## JSX: 将标记引入 JavaScript {/*jsx-putting-markup-into-javascript*/}

The Web has been built on HTML, CSS, and JavaScript. For many years, web developers kept content in HTML, design in CSS, and logic in JavaScript—often in separate files! Content was marked up inside HTML while the page's logic lived separately in JavaScript:
网页是构建在 HTMLCSS JavaScript 之上的。多年以来,web 开发者都是将网页内容存放在 HTML 中,样式放在 CSS 中,而逻辑则放在 JavaScript 中 —— 通常是在不同的文件中!页面的内容通过标记语言描述并存放在 HTML 文件中,而逻辑则单独存放在 JavaScript 文件中。
songhn233 marked this conversation as resolved.
Show resolved Hide resolved

![HTML and JavaScript living in separate files](/images/docs/illustrations/i_html_js.svg)

But as the Web became more interactive, logic increasingly determined content. JavaScript was in charge of the HTML! This is why **in React, rendering logic and markup live together in the same place—components!**
但是随着网页交互复杂度的提升,逻辑愈发决定了内容的展示。JavaScript 开始主导 HTML 显示的内容!这就是为什么 **在 React 中渲染逻辑和标记存放在一个相同的地方 —— 组件!**

![JavaScript functions sprinkled with markup](/images/docs/illustrations/i_jsx.svg)

Keeping a button's rendering logic and markup together ensures that they stay in sync with each other on every edit. Conversely, details that are unrelated, such as the button's markup and a sidebar's markup, are isolated from each other, making it safer to change either of them on their own.
将一个按钮的渲染逻辑和标记放在一起可以确保它们在每次编辑时都能保持互相同步。反之,彼此无关的细节是互相隔离的,例如按钮的标记和侧边栏的标记。这样我们在修改其中任意一个组件时会更安全。

Each React component is a JavaScript function that may contain some markup that React renders into the browser. React components use a syntax extension called JSX to represent that markup. JSX looks a lot like HTML, but it is a bit stricter and can display dynamic information. The best way to understand this is to convert some HTML markup to JSX markup.
每个 React 组件都是一个 JavaScript 函数,它会返回一些标记,React 会将这些标记渲染到浏览器上。React 组件使用一种被称为 JSX 的语法扩展来描述这些标记。JSX 看起来和 HTML 很像,但它的语法更加严格并且可以动态展示信息。了解这些区别最好的方式就是将一些 HTML 标记转化为 JSX 标记。
songhn233 marked this conversation as resolved.
Show resolved Hide resolved

<Note>

[JSX and React are two separate things](/blog/2020/09/22/introducing-the-new-jsx-transform.html#whats-a-jsx-transform) you _can_ use independently of each other.
[JSX and React 是相互独立的](/blog/2020/09/22/introducing-the-new-jsx-transform.html#whats-a-jsx-transform) 你 _可以_ 单独使用它们中的任意一个。

</Note>

## Converting HTML to JSX {/*converting-html-to-jsx*/}
## HTML 转化为 JSX {/*converting-html-to-jsx*/}

Suppose that you have some (perfectly valid) HTML:
假设你现在有一些(完全有效的)HTML 标记:

```html
<h1>Hedy Lamarr's Todos</h1>
<h1>海蒂·拉玛的代办事项</h1>
<img
src="https://i.imgur.com/yXOvdOSs.jpg"
alt="Hedy Lamarr"
class="photo"
>
<ul>
<li>Invent new traffic lights
<li>Rehearse a movie scene
<li>Improve the spectrum technology
<li>发明一种新式交通信号灯
<li>排练一个电影场景
<li>改进频谱技术
</ul>
```

And you want to put it into your component:
而现在想要把这些标记迁移到组件中:
songhn233 marked this conversation as resolved.
Show resolved Hide resolved

```js
export default function TodoList() {
Expand All @@ -64,25 +64,25 @@ export default function TodoList() {
}
```

If you copy and paste it as is, it will not work:
如果直接复制到组件中,并不能正常工作:
songhn233 marked this conversation as resolved.
Show resolved Hide resolved


<Sandpack>

```js
export default function TodoList() {
return (
// This doesn't quite work!
<h1>Hedy Lamarr's Todos</h1>
// 这不起作用!
<h1>海蒂·拉玛的代办事项</h1>
<img
src="https://i.imgur.com/yXOvdOSs.jpg"
alt="Hedy Lamarr"
class="photo"
>
<ul>
<li>Invent new traffic lights
<li>Rehearse a movie scene
<li>Improve the spectrum technology
<li>发明一种新式交通信号灯
<li>排练一个电影场景
<li>改进频谱技术
</ul>
);
}
Expand All @@ -94,25 +94,25 @@ img { height: 90px }

</Sandpack>

This is because JSX is stricter and has a few more rules than HTML! If you read the error messages above, they'll guide you to fix the markup, or you can follow the guide below.
这是因为 JSX 语法更加严格并且相比 HTML 有更多的规则!上面的错误提示可以帮助你修复标记中的错误,当然也可以参考下面的指引。

<Note>

Most of the times, React's on-screen error messages will help you find where the problem is. Give them a read if you get stuck!
大部分情况下,React 在屏幕上显示的错误提示就能帮你找到问题所在,如果在编写过程中遇到问题就参考一下提示吧。

</Note>

## The Rules of JSX {/*the-rules-of-jsx*/}
## JSX 规则 {/*the-rules-of-jsx*/}

### 1. Return a single root element {/*1-return-a-single-root-element*/}
### 1. 只能返回一个根元素 {/*1-return-a-single-root-element*/}

To return multiple elements from a component, **wrap them with a single parent tag**.
如果想要在一个组件中包含多个元素,**需要用一个父标签把它们包裹起来**。

For example, you can use a `<div>`:
例如,你可以使用一个 `<div>` 标签:

```js {1,11}
<div>
<h1>Hedy Lamarr's Todos</h1>
<h1>海蒂·拉玛的代办事项</h1>
<img
src="https://i.imgur.com/yXOvdOSs.jpg"
alt="Hedy Lamarr"
Expand All @@ -125,11 +125,11 @@ For example, you can use a `<div>`:
```


If you don't want to add an extra `<div>` to your markup, you can write `<>` and `</>` instead:
如果你不想在标记中增加一个额外的 `<div>`,可以用 `<>` `</>` 元素来代替:

```js {1,11}
<>
<h1>Hedy Lamarr's Todos</h1>
<h1>海蒂·拉玛的代办事项</h1>
<img
src="https://i.imgur.com/yXOvdOSs.jpg"
alt="Hedy Lamarr"
Expand All @@ -141,19 +141,19 @@ If you don't want to add an extra `<div>` to your markup, you can write `<>` and
</>
```

This empty tag is called a *[React fragment](TODO)*. React fragments let you group things without leaving any trace in the browser HTML tree.
这个空标签被称作 *[React fragment](TODO)*. React fragments 允许你将子元素分组,而不会在 HTML 结构中添加额外节点。

<DeepDive title="Why do multiple JSX tags need to be wrapped?">
<DeepDive title="为什么多个 JSX 标签需要被一个父元素包裹?">

JSX looks like HTML, but under the hood it is transformed into plain JavaScript objects. You can't return two objects from a function without wrapping them into an array. This explains why you also can't return two JSX tags without wrapping them into another tag or a fragment.
JSX 虽然看起来很像 HTML,但在底层其实被转化为了 JavaScript 对象,你不能在一个函数中返回多个对象,除非用一个数组把他们包装起来。这就是为什么多个 JSX 标签必须要用一个父元素或者 fragment 来包裹。

</DeepDive>

### 2. Close all the tags {/*2-close-all-the-tags*/}
### 2. 标签必须闭合 {/*2-close-all-the-tags*/}

JSX requires tags to be explicitly closed: self-closing tags like `<img>` must become `<img />`, and wrapping tags like `<li>oranges` must be written as `<li>oranges</li>`.
JSX 要求标签必须正确闭合。像 `<img>` 这样的自闭合标签必须书写成 `<img />`,而像 `<li>oranges` 这样只有开始标签的元素必须带有闭合标签,需要改为 `<li>oranges</li>`

This is how Hedy Lamarr's image and list items look closed:
海蒂·拉玛的照片和代办事项的标记经修改后变为:

```js {2-6,8-10}
<>
Expand All @@ -163,18 +163,18 @@ This is how Hedy Lamarr's image and list items look closed:
class="photo"
/>
<ul>
<li>Invent new traffic lights</li>
<li>Rehearse a movie scene</li>
<li>Improve the spectrum technology</li>
<li>发明一种新式交通信号灯</li>
<li>排练一个电影场景</li>
<li>改进频谱技术</li>
</ul>
</>
```

### 3. camelCase <s>all</s> most of the things! {/*3-camelcase-salls-most-of-the-things*/}
### 3. 使用驼峰式命名法给 <s>所有</s> 大部分属性命名!{/*3-camelcase-salls-most-of-the-things*/}

JSX turns into JavaScript and attributes written in JSX become keys of JavaScript objects. In your own components, you will often want to read those attributes into variables. But JavaScript has limitations on variable names. For example, their names can't contain dashes or be reserved words like `class`.
JSX 最终会被转化为 JavaScript,而 JSX 中的属性也会变成 JavaScript 对象中的键值对。在你自己的组件中,经常会遇到需要用变量的方式读取这些属性的时候。但 JavaScript 对变量的命名有限制。例如,变量名称不能包含 `-` 符号或者像 `class` 这样的保留字。

This is why, in React, many HTML and SVG attributes are written in camelCase. For example, instead of `stroke-width` you use `strokeWidth`. Since `class` is a reserved word, in React you write `className` instead, named after the [corresponding DOM property](https://developer.mozilla.org/en-US/docs/Web/API/Element/className):
这就是为什么在 React 中,大部分 HTML SVG 属性都用驼峰式命名法表示。例如,需要用 `strokeWidth` 代替 `stroke-width`。由于 `class` 是一个保留字,所以在 React 中需要用 `className` 来代替。这也是 [DOM 属性中的命名](https://developer.mozilla.org/en-US/docs/Web/API/Element/className):

```js {4}
<img
Expand All @@ -184,36 +184,36 @@ This is why, in React, many HTML and SVG attributes are written in camelCase. Fo
/>
```

You can [find all these attributes in the React DOM Elements](TODO). If you get one wrong, don't worry—React will print a message with a possible correction to the [browser console](https://developer.mozilla.org/docs/Tools/Browser_Console).
你可以 [在 React DOM 元素中找到所有对应的属性](TODO)。如果你在编写属性时发生了错误,不用担心 —— React 会在 [浏览器控制台](https://developer.mozilla.org/docs/Tools/Browser_Console) 中打印一条可能的更正信息。

<Gotcha>

For historical reasons, [`aria-*`](https://developer.mozilla.org/docs/Web/Accessibility/ARIA) and [`data-*`](https://developer.mozilla.org/docs/Learn/HTML/Howto/Use_data_attributes) attributes are written as in HTML with dashes.
由于历史原因,[`aria-*`](https://developer.mozilla.org/docs/Web/Accessibility/ARIA) [`data-*`](https://developer.mozilla.org/docs/Learn/HTML/Howto/Use_data_attributes) 属性是以带 `-` 符号的 HTML 格式书写的。

</Gotcha>

### Pro-tip: Use a JSX Converter {/*pro-tip-use-a-jsx-converter*/}
### 高级提示:使用 JSX 转化器 {/*pro-tip-use-a-jsx-converter*/}

Converting all these attributes in existing markup can be tedious! We recommend using a [converter](https://transform.tools/html-to-jsx) to translate your existing HTML and SVG to JSX. Converters are very useful in practice, but it's still worth understanding what is going on so that you can comfortably write JSX on your own.
将现有的 HMTL 中的所有属性转化 JSX 的格式是很繁琐的。我们建议使用 [转化器](https://transform.tools/html-to-jsx) HTML SVG 标记转化为 JSX。这种转化器在实践中非常有用。但我们依然有必要去了解这种转化过程中发生了什么,这样你就可以编写自己的 JSX 了。

Here is your final result:
这是最终的结果:

<Sandpack>

```js
export default function TodoList() {
return (
<>
<h1>Hedy Lamarr's Todos</h1>
<h1>海蒂·拉玛的代办事项</h1>
<img
src="https://i.imgur.com/yXOvdOSs.jpg"
alt="Hedy Lamarr"
className="photo"
/>
<ul>
<li>Invent new traffic lights</li>
<li>Rehearse a movie scene</li>
<li>Improve the spectrum technology</li>
<li>发明一种新式交通信号灯</li>
<li>排练一个电影场景</li>
<li>改进频谱技术</li>
</ul>
</>
);
Expand All @@ -228,34 +228,34 @@ img { height: 90px }

<Recap>

Now you know why JSX exists and how to use it in components:
现在你知道了为什么我们需要 JSX 以及如何在组件中使用它:

* React components group rendering logic together with markup because they are related.
* JSX is similar to HTML, with a few differences. You can use a [converter](https://transform.tools/html-to-jsx) if you need to.
* Error messages will often point you in the right direction to fixing your markup.
* 由于渲染逻辑和标记是紧密相关的,所以 React 将它们存放在一个组件中。
* JSX 类似 HTML,不过有一些区别。如果需要的话可以使用 [转化器](https://transform.tools/html-to-jsx) 将 HTML 转化为 JSX。
* 错误提示通常会指引你将标记修改为正确的格式。

</Recap>



<Challenges>

### Convert some HTML to JSX {/*convert-some-html-to-jsx*/}
### HTML 转化为 JSX {/*convert-some-html-to-jsx*/}

This HTML was pasted into a component, but it's not valid JSX. Fix it:
下方的 HTML 是直接被复制到组件中的,所以并不是有效的 JSX,来尝试修复它吧:

<Sandpack>

```js
export default function Bio() {
return (
<div class="intro">
<h1>Welcome to my website!</h1>
<h1>欢迎来到我的站点!</h1>
</div>
<p class="summary">
You can find my thoughts here.
你可以在这里了解我的想法。
<br><br>
<b>And <i>pictures</b></i> of scientists!
<b>还有科学家们的<i>照片</b></i>
</p>
);
}
Expand All @@ -278,7 +278,7 @@ export default function Bio() {

</Sandpack>

Whether to do it by hand or using the converter is up to you!
你可以随意在手动转化或者使用转化器中选择!

<Solution>

Expand All @@ -289,12 +289,12 @@ export default function Bio() {
return (
<div>
<div className="intro">
<h1>Welcome to my website!</h1>
<h1>欢迎来到我的站点!</h1>
</div>
<p className="summary">
You can find my thoughts here.
你可以在这里了解我的想法。
<br /><br />
<b>And <i>pictures</i></b> of scientists!
<b>还有科学家们的<i>照片</i></b>
</p>
</div>
);
Expand All @@ -320,4 +320,4 @@ export default function Bio() {

</Solution>

</Challenges>
</Challenges>