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: content/docs/components-and-props.md
+49-49
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
id: components-and-props
3
-
title: Components and Props
3
+
title: Components 與 Props
4
4
permalink: docs/components-and-props.html
5
5
redirect_from:
6
6
- "docs/reusable-components.html"
@@ -16,23 +16,23 @@ prev: rendering-elements.html
16
16
next: state-and-lifecycle.html
17
17
---
18
18
19
-
Components let you split the UI into independent, reusable pieces, and think about each piece in isolation. This page provides an introduction to the idea of components. You can find a [detailed component API reference here](/docs/react-component.html).
19
+
Component 使你可以將 UI 拆分成獨立且可複用的程式碼,並且專注於各別程式碼的思考。本章節旨在介紹 component 的相關概念,你也可以在此參閱[詳細的 API 文件](/docs/react-component.html)。
20
20
21
-
Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called "props") and return React elements describing what should appear on the screen.
## Function and Class Components {#function-and-class-components}
23
+
## Function Component 與 Class Component {#function-and-class-components}
24
24
25
-
The simplest way to define a component is to write a JavaScript function:
25
+
定義 component 最簡單的方法即是撰寫一個 Javascript function:
26
26
27
27
```js
28
28
functionWelcome(props) {
29
29
return<h1>Hello, {props.name}</h1>;
30
30
}
31
31
```
32
32
33
-
This function is a valid React component because it accepts a single "props" (which stands for properties) object argument with data and returns a React element. We call such components "function components" because they are literally JavaScript functions.
33
+
此 function 是一個符合規範的 React component,因為它接受一個「props」(指屬性 properties)物件並回傳一個 React element。我們稱之為 function component,因為它本身就是一個 JavaScript function。
34
34
35
-
You can also use an [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes)to define a component:
@@ -42,27 +42,27 @@ class Welcome extends React.Component {
42
42
}
43
43
```
44
44
45
-
The above two components are equivalent from React's point of view.
45
+
上述兩種 component 在 React 中是同等的。
46
46
47
-
Classes have some additional features that we will discuss in the [next sections](/docs/state-and-lifecycle.html). Until then, we will use function components for their conciseness.
47
+
我們將會在[下一個章節]((/docs/state-and-lifecycle.html))探討 class 所擁有的額外特性,但在那之前,我們會使用 function component 來保持簡潔。
48
48
49
-
## Rendering a Component {#rendering-a-component}
49
+
## Render 一個 Component {#rendering-a-component}
50
50
51
-
Previously, we only encountered React elements that represent DOM tags:
51
+
在此之前,我們只見過這種相當於 DOM 標籤的 React element:
52
52
53
53
```js
54
54
constelement=<div />;
55
55
```
56
56
57
-
However, elements can also represent user-defined components:
57
+
不過,React element 也可以是使用者自定義的 component:
58
58
59
59
```js
60
60
constelement=<Welcome name="Sara"/>;
61
61
```
62
62
63
-
When React sees an element representing a user-defined component, it passes JSX attributes to this component as a single object. We call this object "props".
63
+
當 React element 為使用者定義的 component 時,它會將 JSX 所接收的屬性作為一個物件傳遞給 component,這一個物件被稱為「props」。
64
64
65
-
For example, this code renders "Hello, Sara" on the page:
3.`Welcome` component 回傳了 `<h1>Hello, Sara</h1>`這個 element 作為返回值。
86
+
4. React DOM 有效的將 DOM 更新為 `<h1>Hello, Sara</h1>`。
87
87
88
-
>**Note:**Always start component names with a capital letter.
88
+
>**注意:**Component 的字首須為大寫字母
89
89
>
90
-
>React treats components starting with lowercase letters as DOM tags. For example, `<div />`represents an HTML div tag, but `<Welcome />`represents a component and requires `Welcome` to be in scope.
90
+
>React 會將小寫字母開頭的組件視為原始 DOM 標籤,舉例來說,`<div />`就會被視為是 HTML 的 div 標籤,但是 `<Welcome />`則是一個 component,而且需要在作用域中使用 `Welcome`。
91
91
>
92
-
>To learn more about the reasoning behind this convention, please read [JSX In Depth](/docs/jsx-in-depth.html#user-defined-components-must-be-capitalized).
92
+
>想要了解更多關於此慣例的原因,請參閱 [JSX In Depth](/docs/jsx-in-depth.html#user-defined-components-must-be-capitalized) 章節。
93
93
94
-
## Composing Components {#composing-components}
94
+
## 組合 Component {#composing-components}
95
95
96
-
Components can refer to other components in their output. This lets us use the same component abstraction for any level of detail. A button, a form, a dialog, a screen: in React apps, all those are commonly expressed as components.
Typically, new React apps have a single `App` component at the very top. However, if you integrate React into an existing app, you might start bottom-up with a small component like `Button`and gradually work your way to the top of the view hierarchy.
This component can be tricky to change because of all the nesting, and it is also hard to reuse individual parts of it. Let's extract a few components from it.
The`Avatar`doesn't need to know that it is being rendered inside a `Comment`. This is why we have given its prop a more generic name: `user`rather than `author`.
Extracting components might seem like grunt work at first, but having a palette of reusable components pays off in larger apps. A good rule of thumb is that if a part of your UI is used several times (`Button`, `Panel`, `Avatar`), or is complex enough on its own (`App`, `FeedStory`, `Comment`), it is a good candidate to be a reusable component.
Whether you declare a component [as a function or a class](#function-and-class-components), it must never modify its own props. Consider this `sum` function:
240
+
不管你使用 [function 或是 class 來宣告 component](#function-and-class-components),都絕不能修改自己的 props。例如這個 sum function:
241
241
242
242
```js
243
243
functionsum(a, b) {
244
244
return a + b;
245
245
}
246
246
```
247
247
248
-
Such functions are called ["pure"](https://en.wikipedia.org/wiki/Pure_function)because they do not attempt to change their inputs, and always return the same result for the same inputs.
248
+
像這樣的 function 是 [Pure function](https://en.wikipedia.org/wiki/Pure_function)的,因為他們並沒有改變輸入,而且相同的輸入總是回傳一樣的結果。
249
249
250
-
In contrast, this function is impure because it changes its own input:
250
+
相反地,這個 function 並非 Pure function,因為它更改了它的參數:
251
251
252
252
```js
253
253
functionwithdraw(account, amount) {
254
254
account.total-= amount;
255
255
}
256
256
```
257
257
258
-
React is pretty flexible but it has a single strict rule:
258
+
React 是很彈性的,但有一條嚴格的規定:
259
259
260
-
**All React components must act like pure functions with respect to their props.**
260
+
**所有的 React component 都必須像 Pure function 一般保護他的 props**
261
261
262
-
Of course, application UIs are dynamic and change over time. In the [next section](/docs/state-and-lifecycle.html), we will introduce a new concept of "state". State allows React components to change their output over time in response to user actions, network responses, and anything else, without violating this rule.
0 commit comments