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/describing-the-ui.md
+60-53
Original file line number
Diff line number
Diff line change
@@ -1,29 +1,33 @@
1
1
---
2
-
title: Describing the UI
2
+
title: 描述使用者介面
3
3
---
4
4
5
5
<Intro>
6
6
7
-
React is a JavaScript library for rendering user interfaces (UI). UI is built from small units like buttons, text, and images. React lets you combine them into reusable, nestable *components.* From web sites to phone apps, everything on the screen can be broken down into components. In this chapter, you'll learn to create, customize, and conditionally display React components.
React applications are built from isolated pieces of UI called *components*. A React component is a JavaScript function that you can sprinkle with markup. Components can be as small as a button, or as large as an entire page. Here is a `Gallery`component rendering three `Profile`components:
## Importing and exporting components {/*importing-and-exporting-components*/}
65
-
66
-
You can declare many components in one file, but large files can get difficult to navigate. To solve this, you can *export* a component into its own file, and then *import* that component from another file:
Each React component is a JavaScript function that may contain some markup that React renders into the browser. React components use a syntax extension called JSX to represent that markup. JSX looks a lot like HTML, but it is a bit stricter and can display dynamic information.
124
+
每個 React component 都是一個可以渲染一些 Markup 語法到瀏覽器的 Javascript 函數,React components 使用一個語法擴充功能稱作 JSX 來表示這些 Markup 語法。JSX 看起來就像 HTML 一樣,但他比 HTML 更加嚴格一些,並且能夠顯示出動態的資訊。
122
125
123
-
If we paste existing HTML markup into a React component, it won't always work:
126
+
如果你把一個 HTML Markup 直接貼到一個 React component 中,他不一定可以正常運作:
124
127
125
128
<Sandpack>
126
129
127
130
```js
128
131
exportdefaultfunctionTodoList() {
129
132
return (
130
-
//This doesn't quite work!
131
-
<h1>Hedy Lamarr's Todos</h1>
133
+
//這個寫法沒辦法正常運作!
134
+
<h1>Hedy Lamarr 的待辦事項</h1>
132
135
<img
133
136
src="https://i.imgur.com/yXOvdOSs.jpg"
134
137
alt="Hedy Lamarr"
@@ -149,15 +152,15 @@ img { height: 90px; }
149
152
150
153
</Sandpack>
151
154
152
-
If you have existing HTML like this, you can fix it using a [converter](https://transform.tools/html-to-jsx):
JSX lets you write HTML-like markup inside a JavaScript file, keeping rendering logic and content in the same place. Sometimes you will want to add a little JavaScript logic or reference a dynamic property inside that markup. In this situation, you can use curly braces in your JSX to "open a window" to JavaScript:
193
+
JSX 讓你可以在 Javascript 檔案中使用類似 HTML 的語法,這讓我們把渲染邏輯和內容能夠寫在同一個地方。有時候你可能想要在 Markup 中使用一些 Javascript 的邏輯或是引入一個動態的變數。
React components use *props* to communicate with each other. Every parent component can pass some information to its child components by giving them props. Props might remind you of HTML attributes, but you can pass any JavaScript value through them, including objects, arrays, functions, and even JSX!
245
+
Props 可能會讓你想起 HTML 語法中的 attributes,但你可以傳遞任何 JavaScript 的變數到 Props 中,包括物件、陣列、函數,甚至是 JSX!
Your components will often need to display different things depending on different conditions. In React, you can conditionally render JSX using JavaScript syntax like `if`statements, `&&`, and`? :`operators.
You will often want to display multiple similar components from a collection of data. You can use JavaScript's `filter()`and`map()`with React to filter and transform your array of data into an array of components.
For each array item, you will need to specify a `key`. Usually, you will want to use an IDfrom the database as a `key`. Keyslet React keep track of each item's place in the list even if the list changes.
376
+
對轉換後的陣列中的每個物件,你必須要幫他們指定一個 `key`。 一般來說,你可以用他們在資料庫中的 ID 來作為 `key`。 Key 可以讓 React 知道列表中每個物件當前的位置,即便你的列表發生了改動也沒關係。
370
377
371
378
<Sandpack>
372
379
@@ -390,7 +397,7 @@ export default function List() {
390
397
);
391
398
return (
392
399
<article>
393
-
<h1>Scientists</h1>
400
+
<h1>科學家列表</h1>
394
401
<ul>{listItems}</ul>
395
402
</article>
396
403
);
@@ -458,18 +465,18 @@ h2 { font-size: 20px; }
458
465
459
466
<LearnMorepath="/learn/rendering-lists">
460
467
461
-
Read**[Rendering Lists](/learn/rendering-lists)**to learn how to render a list of components, and how to choose a key.
***Minds its own business.**It does not change any objects or variables that existed before it was called.
470
-
***Same inputs, same output.**Given the same inputs, a pure function should always return the same result.
476
+
***不多管閒事**: 這個函數不會修改任何在他被呼叫之前就已經存在的物件或變數。
477
+
***一樣的輸入,一樣的輸出**: 只要我們輸入相同的參數,這個函數總是回傳一個相同的輸出。
471
478
472
-
By strictly only writing your components as pure functions, you can avoid an entire class of baffling bugs and unpredictable behavior as your codebase grows. Here is an example of an impure component:
0 commit comments