Skip to content

Commit dad3fe4

Browse files
authored
Merge pull request #12 from bugtender/translate-components-and-props-zh_hant
docs(zh-hant): Translate Components and Props
2 parents 20c18e2 + b2c6dfb commit dad3fe4

File tree

2 files changed

+50
-50
lines changed

2 files changed

+50
-50
lines changed

content/docs/components-and-props.md

+49-49
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: components-and-props
3-
title: Components and Props
3+
title: Components Props
44
permalink: docs/components-and-props.html
55
redirect_from:
66
- "docs/reusable-components.html"
@@ -16,23 +16,23 @@ prev: rendering-elements.html
1616
next: state-and-lifecycle.html
1717
---
1818

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)
2020

21-
Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called "props") and return React elements describing what should appear on the screen.
21+
概念上來說,component 就像是 JavaScript 的 function,它接收任意的參數(稱之為「props」)並且回傳描述畫面的 React element。
2222

23-
## Function and Class Components {#function-and-class-components}
23+
## Function Component 與 Class Component {#function-and-class-components}
2424

25-
The simplest way to define a component is to write a JavaScript function:
25+
定義 component 最簡單的方法即是撰寫一個 Javascript function
2626

2727
```js
2828
function Welcome(props) {
2929
return <h1>Hello, {props.name}</h1>;
3030
}
3131
```
3232

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。
3434

35-
You can also use an [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) to define a component:
35+
同樣的,你也可以使用 [ES6 Class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) 來定義 component
3636

3737
```js
3838
class Welcome extends React.Component {
@@ -42,27 +42,27 @@ class Welcome extends React.Component {
4242
}
4343
```
4444

45-
The above two components are equivalent from React's point of view.
45+
上述兩種 component 在 React 中是同等的。
4646

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 來保持簡潔。
4848

49-
## Rendering a Component {#rendering-a-component}
49+
## Render 一個 Component {#rendering-a-component}
5050

51-
Previously, we only encountered React elements that represent DOM tags:
51+
在此之前,我們只見過這種相當於 DOM 標籤的 React element:
5252

5353
```js
5454
const element = <div />;
5555
```
5656

57-
However, elements can also represent user-defined components:
57+
不過,React element 也可以是使用者自定義的 component:
5858

5959
```js
6060
const element = <Welcome name="Sara" />;
6161
```
6262

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」。
6464

65-
For example, this code renders "Hello, Sara" on the page:
65+
舉例來說,這段程式碼會在頁面上 render 出「Hello, Sara」:
6666

6767
```js{1,5}
6868
function Welcome(props) {
@@ -76,26 +76,26 @@ ReactDOM.render(
7676
);
7777
```
7878

79-
[](codepen://components-and-props/rendering-a-component)
79+
[在 CodePen 上試試看吧!](codepen://components-and-props/rendering-a-component)
8080

81-
Let's recap what happens in this example:
81+
讓我們來複習一下這個例子發生了什麼事:
8282

83-
1. We call `ReactDOM.render()` with the `<Welcome name="Sara" />` element.
84-
2. React calls the `Welcome` component with `{name: 'Sara'}` as the props.
85-
3. Our `Welcome` component returns a `<h1>Hello, Sara</h1>` element as the result.
86-
4. React DOM efficiently updates the DOM to match `<h1>Hello, Sara</h1>`.
83+
1. 我們對 `<Welcome name="Sara" />` 這個 element 呼叫了 `ReactDOM.render()`
84+
2. React `{name: 'Sara'}` 作為 props 傳入 `Welcome` component 並呼叫。
85+
3. `Welcome` component 回傳了 `<h1>Hello, Sara</h1>` 這個 element 作為返回值。
86+
4. React DOM 有效的將 DOM 更新為 `<h1>Hello, Sara</h1>`
8787

88-
>**Note:** Always start component names with a capital letter.
88+
>**注意:** Component 的字首須為大寫字母
8989
>
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`
9191
>
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) 章節。
9393
94-
## Composing Components {#composing-components}
94+
## 組合 Component {#composing-components}
9595

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.
96+
Component 可以在輸出中引用其他 component。我們可以在任何層次中抽象化相同的 component,按鈕、表單、對話框、甚至是整個畫面,在 React 應用程式中都將以 component 的方式呈現。
9797

98-
For example, we can create an `App` component that renders `Welcome` many times:
98+
舉例來說,我們可以建立一個 render 多次 `Welcome``App` component:
9999

100100
```js{8-10}
101101
function Welcome(props) {
@@ -118,15 +118,15 @@ ReactDOM.render(
118118
);
119119
```
120120

121-
[](codepen://components-and-props/composing-components)
121+
[在 CodePen 上試試看吧!](codepen://components-and-props/composing-components)
122122

123-
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.
123+
通常來說,每個 React 應用程式都有一個最高層級的 `App` component。然而,如果你將 React 結合至現存的應用程式中,你可能需要使用像 `Button` 這樣的小型 component,並由下往上,逐步應用到畫面的最高層級。
124124

125-
## Extracting Components {#extracting-components}
125+
## 抽取 Component {#extracting-components}
126126

127-
Don't be afraid to split components into smaller components.
127+
別害怕將 component 拆分成更小的 component。
128128

129-
For example, consider this `Comment` component:
129+
舉例來說,我們看這個 `Comment` component
130130

131131
```js
132132
function Comment(props) {
@@ -152,13 +152,13 @@ function Comment(props) {
152152
}
153153
```
154154

155-
[](codepen://components-and-props/extracting-components)
155+
[在 CodePen 上試試看吧!](codepen://components-and-props/extracting-components)
156156

157-
It accepts `author` (an object), `text` (a string), and `date` (a date) as props, and describes a comment on a social media website.
157+
它接受 `author` (一個物件)、`text` (一個字串)、還有 `date` (一個日期) 作為它的 props。它的作用是在一個社交網站上 render 一則評論。
158158

159-
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.
159+
這個 component 可能因為太多的巢狀關係而難以更動,而且也難以複用獨立的部分。讓我們把一些 component 從中分離吧。
160160

161-
First, we will extract `Avatar`:
161+
首先, 我們將 `Avatar` 分離出來:
162162

163163
```js{3-6}
164164
function Avatar(props) {
@@ -171,11 +171,11 @@ function Avatar(props) {
171171
}
172172
```
173173

174-
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`.
174+
這個 `Avatar` 並不需知道它會被 render 在 `Comment` 中。這是為什麼我們給他一個更為一般的名字:`user` 而不是 `author`
175175

176-
We recommend naming props from the component's own point of view rather than the context in which it is being used.
176+
我們建議從 component 的角度為 props 命名,而不是它的使用情境。
177177

178-
We can now simplify `Comment` a tiny bit:
178+
現在我們可以稍微簡化 `Comment`
179179

180180
```js{5}
181181
function Comment(props) {
@@ -198,7 +198,7 @@ function Comment(props) {
198198
}
199199
```
200200

201-
Next, we will extract a `UserInfo` component that renders an `Avatar` next to the user's name:
201+
接下來,我們將 `UserInfo` component 也抽離出來,它會在使用者名稱旁邊 render `Avatar` component:
202202

203203
```js{3-8}
204204
function UserInfo(props) {
@@ -213,7 +213,7 @@ function UserInfo(props) {
213213
}
214214
```
215215

216-
This lets us simplify `Comment` even further:
216+
讓我們將 `Comment` 更加簡化:
217217

218218
```js{4}
219219
function Comment(props) {
@@ -231,32 +231,32 @@ function Comment(props) {
231231
}
232232
```
233233

234-
[](codepen://components-and-props/extracting-components-continued)
234+
[在 CodePen 上試試看吧!](codepen://components-and-props/extracting-components-continued)
235235

236-
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.
236+
在一開始,將 component 抽離出來可能是一件繁重的工作,但是在較大的應用程式中,建構可複用的 component 是非常值得。以經驗來說,如果一個 UI 中有一部分會被重複使用很多次(`Button``Panel``Avatar`),或者它足夠複雜到自成一個 component(`App``FeedStory``Comment`),那它就適合被抽出作為一個可複用的 component
237237

238-
## Props are Read-Only {#props-are-read-only}
238+
## Props 是唯讀的 {#props-are-read-only}
239239

240-
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
241241

242242
```js
243243
function sum(a, b) {
244244
return a + b;
245245
}
246246
```
247247

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) 的,因為他們並沒有改變輸入,而且相同的輸入總是回傳一樣的結果。
249249

250-
In contrast, this function is impure because it changes its own input:
250+
相反地,這個 function 並非 Pure function,因為它更改了它的參數:
251251

252252
```js
253253
function withdraw(account, amount) {
254254
account.total -= amount;
255255
}
256256
```
257257

258-
React is pretty flexible but it has a single strict rule:
258+
React 是很彈性的,但有一條嚴格的規定:
259259

260-
**All React components must act like pure functions with respect to their props.**
260+
**所有的 React component 都必須像 Pure function 一般保護他的 props**
261261

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.
262+
當然,應用程式的 UI 是動態的,而且總是隨著時間改變。在[下個章節](/docs/state-and-lifecycle.html),我們會介紹一個新的概念「state」。State 可以在不違反上述規則的前提下,讓 React component 隨使用者操作、網路回應、或是其他方式改變輸出內容。

content/docs/nav.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
- id: rendering-elements
1919
title: Rendering Elements
2020
- id: components-and-props
21-
title: Components and Props
21+
title: Components Props
2222
- id: state-and-lifecycle
2323
title: State and Lifecycle
2424
- id: handling-events

0 commit comments

Comments
 (0)