You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: beta/src/pages/learn/your-first-component.md
+74-72
Original file line number
Diff line number
Diff line change
@@ -1,47 +1,49 @@
1
1
---
2
-
title: Your First Component
2
+
title: 你的第一个组件
3
+
translators:
4
+
- fine-bot
3
5
---
4
6
5
7
<Intro>
6
8
7
-
Components are one of the core concepts of React. They are the foundation upon which you build user interfaces (UI), which makes them the perfect place to start your React journey!
## Components: UI building blocks {/*components-ui-building-blocks*/}
21
+
## 组件:UI 构成要素 {/*components-ui-building-blocks*/}
20
22
21
-
On the Web, HTML lets us create rich structured documents with its built-in set of tags like `<h1>`and`<li>`:
23
+
在 Web 当中,HTML 允许我们使用其内置的标签集(如 `<h1>`和`<li>`)创建丰富的结构化文档:
22
24
23
25
```html
24
26
<article>
25
-
<h1>My First Component</h1>
27
+
<h1>我的第一个组件</h1>
26
28
<ol>
27
-
<li>Components: UI Building Blocks</li>
28
-
<li>Defining a Component</li>
29
-
<li>Using a Component</li>
29
+
<li>组件:UI 构成要素</li>
30
+
<li>定义组件</li>
31
+
<li>使用组件</li>
30
32
</ol>
31
33
</article>
32
34
```
33
35
34
-
This markup represents this article `<article>`, its heading `<h1>`, and an (abbreviated) table of contents as an ordered list `<ol>`. Markup like this, combined with CSS for style, and JavaScript for interactivity, lies behind every sidebar, avatar, modal, dropdown—every piece of UI you see on the Web.
React lets you combine your markup, CSS, and JavaScript into custom "components," **reusable UI elements for your app.**The table of contents code you saw above could be turned into a `<TableOfContents />`component you could render on every page. Under the hood, it still uses the same HTML tags like `<article>`, `<h1>`, etc.
Just like with HTML tags, you can compose, order and nest components to design whole pages. For example, the documentation page you're reading is made out of React components:
40
+
就像使用 HTML 标签一样,你可以组合、排序和嵌套组件来绘制整个页面。例如,你正在阅读的文档页面就是由 React 组件构成的:
39
41
40
42
```js
41
43
<PageLayout>
42
44
<NavigationHeader>
43
45
<SearchBar />
44
-
<Link to="/docs">Docs</Link>
46
+
<Link to="/docs">文档</Link>
45
47
</NavigationHeader>
46
48
<Sidebar />
47
49
<PageContent>
@@ -51,11 +53,11 @@ Just like with HTML tags, you can compose, order and nest components to design w
51
53
</PageLayout>
52
54
```
53
55
54
-
As your project grows, you will notice that many of your designs can be composed by reusing components you already wrote, speeding up your development. Our table of contents above could be added to any screen with `<TableOfContents />`! You can even jumpstart your project with the thousands of components shared by the React open source community like [Chakra UI](https://chakra-ui.com/)and[Material UI](https://material-ui.com/).
## Defining a component {/*defining-a-component*/}
58
+
## 定义组件 {/*defining-a-component*/}
57
59
58
-
Traditionally when creating web pages, web developers marked up their content and then added interaction by sprinkling on some JavaScript. This worked great when interaction was a nice-to-have on the web. Now it is expected for many sites and all apps. React puts interactivity first while still using the same technology: **a React component is a JavaScript function that you can _sprinkle with markup_**. Here's what that looks like (you can edit the example below):
### Step 1: Export the component {/*step-1-export-the-component*/}
83
+
### 第一步:导出组件 {/*step-1-export-the-component*/}
82
84
83
-
The `export default`prefix is a [standard JavaScript syntax](https://developer.mozilla.org/docs/web/javascript/reference/statements/export) (not specific to React). It lets you mark the main function in a file so that you can later import it from other files. (More on importing in [Importing and Exporting Components](/learn/importing-and-exporting-components)!)
The component returns an `<img />` tag with `src`and`alt`attributes.`<img />`is written like HTML, but it is actually JavaScript under the hood! This syntax is called [JSX](/learn/writing-markup-with-jsx), and it lets you embed markup inside JavaScript.
But if your markup isn't all on the same line as the return statement, you must wrap it in a pair of parentheses like this:
107
+
但是,如果你的标记和返回语句不在同一行,则必须把它包裹在一对括号中,如下所示:
106
108
107
109
```js
108
110
return (
@@ -114,13 +116,13 @@ return (
114
116
115
117
<Gotcha>
116
118
117
-
Without parentheses, any code on the lines after `return`[will be ignored](https://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi)!
Now that you've defined your `Profile`component, you can nest it inside other components. For example, you can export a `Gallery` component that uses multiple `Profile` components:
Components are regular JavaScript functions, so you can keep multiple components in the same file. This is convenient when components are relatively small or tightly related to each other. If this file gets crowded, you can always move `Profile`to a separate file. You will learn how to do this shortly on the [page about imports](/learn/importing-and-exporting-components).
Because the `Profile`components are rendered inside `Gallery`—even several times!—we can say that `Gallery`is a **parent component,**rendering each `Profile`as a "child". This is part of the magic of React: you can define a component once, and then use it in as many places and as many times as you like.
Your React application begins at a "root" component. Usually, it is created automatically when you start a new project. For example, if you use [CodeSandbox](https://codesandbox.io/)or[Create React App](https://create-react-app.dev/), the root component is defined in `src/App.js`. If you use the framework [Next.js](https://nextjs.org/), the root component is defined in `pages/index.js`. In these examples, you've been exporting root components.
Most React apps use components all the way down. This means that you won't only use components for reusable pieces like buttons, but also for larger pieces like sidebars, lists, and ultimately, complete pages! Components are a handy way to organize UI code and markup, even if some of them are only used once.
Frameworks like Next.js take this a step further. Instead of using an empty HTML file and letting React "take over" managing the page with JavaScript, they *also* generate the HTML automatically from your React components. This allows your app to show some content before the JavaScript code loads.
Still, many websites only use React to [add "sprinkles of interactivity"](/learn/add-react-to-a-website). They have many root components instead of a single one for the entire page. You can use as much—or as little—React as you need.
You've just gotten your first taste of React! Let's recap some key points.
195
+
你刚刚第一次体验 React!让我们回顾一些关键点。
194
196
195
-
* React lets you create components, **reusable UI elements for your app.**
196
-
*In a React app, every piece of UI is a component.
197
-
* React components are regular JavaScript functions except:
197
+
* React 允许你创建组件,**应用程序的可复用 UI 元素。**
198
+
*在 React 应用程序中,每一个 UI 模块都是一个组件。
199
+
* React 是常规的 JavaScript 函数,除了:
198
200
199
-
1.Their names always begin with a capital letter.
200
-
2.They return JSX markup.
201
+
1.它们的名字总是以大写字母开头。
202
+
2.它们返回 JSX 标记。
201
203
202
204
</Recap>
203
205
204
206
205
207
206
208
<Challenges>
207
209
208
-
### Export the component {/*export-the-component*/}
210
+
### 导出组件 {/*export-the-component*/}
209
211
210
-
This sandbox doesn't work because the root component is not exported:
212
+
这个沙箱不起作用,因为根组件没有导出:
211
213
212
214
<Sandpack>
213
215
@@ -228,11 +230,11 @@ img { height: 181px; }
228
230
229
231
</Sandpack>
230
232
231
-
Try to fix it yourself before looking at the solution!
233
+
看答案之前先尝试自己修复它!
232
234
233
235
<Solution>
234
236
235
-
Add`export default` before the function definition like so:
237
+
在函数定义前添加`export default`,如下所示:
236
238
237
239
<Sandpack>
238
240
@@ -253,17 +255,17 @@ img { height: 181px; }
253
255
254
256
</Sandpack>
255
257
256
-
You might be wondering why writing `export`alone is not enough to fix this example. You can learn the difference between `export` and `export default` in [Importing and Exporting Components](/learn/importing-and-exporting-components).
### Fix the return statement {/*fix-the-return-statement*/}
262
+
### 修复返回语句 {/*fix-the-return-statement*/}
261
263
262
-
Something isn't right about this `return`statement. Can you fix it?
264
+
这个 `return`语句不太对,你能修复它吗?
263
265
264
266
<Hint>
265
267
266
-
You may get an "Unexpected token" error while trying to fix this. In that case, check the that semicolon appears *after* the closing parenthesis. Leaving a semicolon inside `return ( )`will cause an error.
You can fix this component by moving the return statement to one line like so:
290
+
你可以通过将返回语句移动到一行上来修复这个组件,如下所示:
289
291
290
292
<Sandpack>
291
293
@@ -301,7 +303,7 @@ img { height: 180px; }
301
303
302
304
</Sandpack>
303
305
304
-
Or by wrapping the returned JSX markup in parentheses that open right after `return`:
306
+
或者用括号包裹返回的 JSX 标记,将左括号放在 `return` 的后面:
305
307
306
308
<Sandpack>
307
309
@@ -324,9 +326,9 @@ img { height: 180px; }
324
326
325
327
</Solution>
326
328
327
-
### Spot the mistake {/*spot-the-mistake*/}
329
+
### 发现错误 {/*spot-the-mistake*/}
328
330
329
-
Something's wrong with how the `Profile`component is declared and used. Can you spot the mistake? (Try to remember how React distinguishes components from the regular HTML tags!)
331
+
下面 `Profile`组件的声明和使用存在问题。你能指出其中的错误所在吗?(试着想起 React 是如何区分组件和常规的 HTML 标签的!)
Write a component from scratch. You can give it any valid name and return any markup. If you're out of ideas, you can write a `Congratulations` component thats shows `<h1>Good job!</h1>`. Don't forget to export it!
0 commit comments