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: src/content/learn/your-first-component.md
+64-63Lines changed: 64 additions & 63 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,24 +1,24 @@
1
1
---
2
-
title: Your First Component
2
+
title: 你的第一個 Component
3
3
---
4
4
5
5
<Intro>
6
6
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!
@@ -31,11 +31,11 @@ On the Web, HTML lets us create rich structured documents with its built-in set
31
31
</article>
32
32
```
33
33
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:
38
+
就像是 HTML 標籤一樣,你可以編寫、排序以及使用巢狀結構來設計整個頁面。例如,你正在看閱讀的文件頁面就是由 React component 所組成:
39
39
40
40
```js
41
41
<PageLayout>
@@ -51,11 +51,11 @@ Just like with HTML tags, you can compose, order and nest components to design w
51
51
</PageLayout>
52
52
```
53
53
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*/}
56
+
## 定義 component {/*defining-a-component*/}
57
57
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*/}
81
+
### 第一步:Export the component {/*step-1-export-the-component*/}
82
82
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.
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.
@@ -190,7 +190,7 @@ export default function Gallery() {
190
190
}
191
191
```
192
192
193
-
The snippet above is [very slow and causes bugs.](/learn/preserving-and-resetting-state#different-components-at-the-same-position-reset-state) Instead, define every component at the top level:
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 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.
215
214
216
-
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.
[React-based frameworks](/learn/start-a-new-react-project) 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 interactivity to existing HTML pages.](/learn/add-react-to-an-existing-project#using-react-for-a-part-of-your-existing-page) 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.
227
+
你剛剛第一次體驗了 React!讓我們來回顧一些重點。
227
228
228
-
* React lets you create components, **reusable UI elements for your app.**
229
-
*In a React app, every piece of UI is a component.
230
-
* React components are regular JavaScript functions except:
229
+
* React 允許你創建 component,**應用程式中可複用的 UI 元素**。
230
+
*在 React 應用程式中,所有的 UI 模塊都是一個 component。
231
+
* React component 是常規的 JavaScript 函式,除了:
231
232
232
-
1.Their names always begin with a capital letter.
233
-
2.They return JSX markup.
233
+
1.它們的名字總是以大寫字母為開頭。
234
+
2.它們回傳 JSX markup。
234
235
235
236
</Recap>
236
237
@@ -240,7 +241,7 @@ You've just gotten your first taste of React! Let's recap some key points.
240
241
241
242
#### Export the component {/*export-the-component*/}
242
243
243
-
This sandbox doesn't work because the root component is not exported:
244
+
由於 root component 沒有被 export,導致這個沙箱無法運作:
244
245
245
246
<Sandpack>
246
247
@@ -261,11 +262,11 @@ img { height: 181px; }
261
262
262
263
</Sandpack>
263
264
264
-
Try to fix it yourself before looking at the solution!
265
+
請試著在查閱解答前自行修復它!
265
266
266
267
<Solution>
267
268
268
-
Add`export default` before the function definition like so:
269
+
在函式定義前加上`export default`,如下所示:
269
270
270
271
<Sandpack>
271
272
@@ -286,17 +287,17 @@ img { height: 181px; }
286
287
287
288
</Sandpack>
288
289
289
-
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*/}
294
+
#### 修復回傳內容 {/*fix-the-return-statement*/}
294
295
295
-
Something isn't right about this `return`statement. Can you fix it?
296
+
`return`內容出了點問題,你能夠修復它嗎?
296
297
297
298
<Hint>
298
299
299
-
You may get an "Unexpected token" error while trying to fix this. In that case, check that the 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:
322
+
你可以透過將回傳的內容移至同一行來修復這個問題,如下:
322
323
323
324
<Sandpack>
324
325
@@ -334,7 +335,7 @@ img { height: 180px; }
334
335
335
336
</Sandpack>
336
337
337
-
Or by wrapping the returned JSX markup in parentheses that open right after `return`:
338
+
或者使用括號包裹回傳的 JSX markup,將左括號放在 `return` 的後面:
338
339
339
340
<Sandpack>
340
341
@@ -357,9 +358,9 @@ img { height: 180px; }
357
358
358
359
</Solution>
359
360
360
-
#### Spot the mistake {/*spot-the-mistake*/}
361
+
#### 發現錯誤 {/*spot-the-mistake*/}
361
362
362
-
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!)
363
+
`Profile` component 的宣告及使用發生了問題,你能指出其中的錯誤嗎?(試著回憶 React 是如何判別 component 與常規的 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