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
+65-63
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 +191,7 @@ export default function Gallery() {
190
191
}
191
192
```
192
193
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
215
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.
228
+
你剛剛第一次體驗了 React!讓我們來回顧一些重點。
227
229
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:
230
+
* React 允許你建立 component,**應用程式中可複用的 UI 元素**。
231
+
*在 React 應用程式中,所有的 UI 模塊都是一個 component。
232
+
* React component 是常規的 JavaScript 函式,除了:
231
233
232
-
1.Their names always begin with a capital letter.
233
-
2.They return JSX markup.
234
+
1.它們的名字總是以大寫字母為開頭。
235
+
2.它們回傳 JSX markup。
234
236
235
237
</Recap>
236
238
@@ -240,7 +242,7 @@ You've just gotten your first taste of React! Let's recap some key points.
240
242
241
243
#### Export the component {/*export-the-component*/}
242
244
243
-
This sandbox doesn't work because the root component is not exported:
245
+
由於 root component 沒有被 export,導致這個 sandbox 無法運作:
244
246
245
247
<Sandpack>
246
248
@@ -261,11 +263,11 @@ img { height: 181px; }
261
263
262
264
</Sandpack>
263
265
264
-
Try to fix it yourself before looking at the solution!
266
+
請試著在查閱解答前自行修復它!
265
267
266
268
<Solution>
267
269
268
-
Add`export default` before the function definition like so:
270
+
在函式定義前加上`export default`,如下所示:
269
271
270
272
<Sandpack>
271
273
@@ -286,17 +288,17 @@ img { height: 181px; }
286
288
287
289
</Sandpack>
288
290
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*/}
295
+
#### 修復回傳內容 {/*fix-the-return-statement*/}
294
296
295
-
Something isn't right about this `return`statement. Can you fix it?
297
+
`return`內容出了點問題,你能夠修復它嗎?
296
298
297
299
<Hint>
298
300
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:
323
+
你可以透過將回傳的內容移至同一行來修復這個問題,如下:
322
324
323
325
<Sandpack>
324
326
@@ -334,7 +336,7 @@ img { height: 180px; }
334
336
335
337
</Sandpack>
336
338
337
-
Or by wrapping the returned JSX markup in parentheses that open right after `return`:
339
+
或者使用括號包裹回傳的 JSX markup,將左括號放在 `return` 的後面:
338
340
339
341
<Sandpack>
340
342
@@ -357,9 +359,9 @@ img { height: 180px; }
357
359
358
360
</Solution>
359
361
360
-
#### Spot the mistake {/*spot-the-mistake*/}
362
+
#### 發現錯誤 {/*spot-the-mistake*/}
361
363
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!)
364
+
`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