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
+76-72Lines changed: 76 additions & 72 deletions
Original file line number
Diff line number
Diff line change
@@ -1,47 +1,51 @@
1
1
---
2
-
title: Your First Component
2
+
title: 你的第一个组件
3
+
translators:
4
+
- fine-bot
5
+
- nmsn
6
+
- QC-L
3
7
---
4
8
5
9
<Intro>
6
10
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*/}
23
+
## 组件:UI 构成要素 {/*components-ui-building-blocks*/}
20
24
21
-
On the Web, HTML lets us create rich structured documents with its built-in set of tags like `<h1>`and`<li>`:
25
+
在 Web 当中,HTML 允许我们使用其内置的标签集(如 `<h1>`和`<li>`)创建丰富的结构化文档:
22
26
23
27
```html
24
28
<article>
25
-
<h1>My First Component</h1>
29
+
<h1>我的第一个组件</h1>
26
30
<ol>
27
-
<li>Components: UI Building Blocks</li>
28
-
<li>Defining a Component</li>
29
-
<li>Using a Component</li>
31
+
<li>组件:UI 构成要素</li>
32
+
<li>定义组件</li>
33
+
<li>使用组件</li>
30
34
</ol>
31
35
</article>
32
36
```
33
37
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:
42
+
就像使用 HTML 标签一样,你可以组合、排序和嵌套组件来绘制整个页面。例如,你正在阅读的文档页面就是由 React 组件构成的:
39
43
40
44
```js
41
45
<PageLayout>
42
46
<NavigationHeader>
43
47
<SearchBar />
44
-
<Link to="/docs">Docs</Link>
48
+
<Link to="/docs">文档</Link>
45
49
</NavigationHeader>
46
50
<Sidebar />
47
51
<PageContent>
@@ -51,11 +55,11 @@ Just like with HTML tags, you can compose, order and nest components to design w
51
55
</PageLayout>
52
56
```
53
57
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*/}
60
+
## 定义组件 {/*defining-a-component*/}
57
61
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*/}
85
+
### 第一步:导出组件 {/*step-1-export-the-component*/}
82
86
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`keyword, you must wrap it in a pair of parentheses like this:
109
+
但是,如果你的标记和 `return`关键字不在同一行,则必须把它包裹在一对括号中,如下所示:
106
110
107
111
```js
108
112
return (
@@ -114,13 +118,13 @@ return (
114
118
115
119
<Gotcha>
116
120
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.
197
+
你刚刚第一次体验 React!让我们回顾一些关键点。
194
198
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:
199
+
* React 允许你创建组件,**应用程序的可复用 UI 元素。**
200
+
*在 React 应用程序中,每一个 UI 模块都是一个组件。
201
+
* React 是常规的 JavaScript 函数,除了:
198
202
199
-
1.Their names always begin with a capital letter.
200
-
2.They return JSX markup.
203
+
1.它们的名字总是以大写字母开头。
204
+
2.它们返回 JSX 标记。
201
205
202
206
</Recap>
203
207
204
208
205
209
206
210
<Challenges>
207
211
208
-
### Export the component {/*export-the-component*/}
212
+
### 导出组件 {/*export-the-component*/}
209
213
210
-
This sandbox doesn't work because the root component is not exported:
214
+
这个沙箱不起作用,因为根组件没有导出:
211
215
212
216
<Sandpack>
213
217
@@ -228,11 +232,11 @@ img { height: 181px; }
228
232
229
233
</Sandpack>
230
234
231
-
Try to fix it yourself before looking at the solution!
235
+
看答案之前先尝试自己修复它!
232
236
233
237
<Solution>
234
238
235
-
Add`export default` before the function definition like so:
239
+
在函数定义前添加`export default`,如下所示:
236
240
237
241
<Sandpack>
238
242
@@ -253,17 +257,17 @@ img { height: 181px; }
253
257
254
258
</Sandpack>
255
259
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*/}
264
+
### 修复返回语句 {/*fix-the-return-statement*/}
261
265
262
-
Something isn't right about this `return`statement. Can you fix it?
266
+
这个 `return`语句不太对,你能修复它吗?
263
267
264
268
<Hint>
265
269
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:
292
+
你可以通过将返回语句移动到一行上来修复这个组件,如下所示:
289
293
290
294
<Sandpack>
291
295
@@ -301,7 +305,7 @@ img { height: 180px; }
301
305
302
306
</Sandpack>
303
307
304
-
Or by wrapping the returned JSX markup in parentheses that open right after `return`:
308
+
或者用括号包裹返回的 JSX 标记,将左括号放在 `return` 的后面:
305
309
306
310
<Sandpack>
307
311
@@ -324,9 +328,9 @@ img { height: 180px; }
324
328
325
329
</Solution>
326
330
327
-
### Spot the mistake {/*spot-the-mistake*/}
331
+
### 发现错误 {/*spot-the-mistake*/}
328
332
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!)
333
+
下面 `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 that shows `<h1>Good job!</h1>`. Don't forget to export it!
0 commit comments