Skip to content

Commit c950de9

Browse files
committed
docs: translation JavaScript in JSX
1 parent b1d6c52 commit c950de9

File tree

1 file changed

+56
-56
lines changed

1 file changed

+56
-56
lines changed

beta/src/content/learn/javascript-in-jsx-with-curly-braces.md

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
---
2-
title: JavaScript in JSX with Curly Braces
2+
title: JSX 中使用 JavaScript 的語法
33
---
44

55
<Intro>
66

7-
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.
7+
JSX 允許你在 JavaScript 文件中編寫類似 HTML 標籤的代碼,使渲染邏輯和內容保持在同樣的地方。有時你會想在標籤中添加一些 JavaScript 邏輯或引用一個動態屬性。在這種情況下,你可以在 JSX 中使用花括號來編寫 JavaScript
88

99
</Intro>
1010

1111
<YouWillLearn>
1212

13-
* How to pass strings with quotes
14-
* How to reference a JavaScript variable inside JSX with curly braces
15-
* How to call a JavaScript function inside JSX with curly braces
16-
* How to use a JavaScript object inside JSX with curly braces
13+
* 如何使用引號傳遞 string
14+
* 如何使用花括號在 JSX 中引用 JavaScript 變量
15+
* 如何使用花括號在 JSX 中調用 JavaScript 函數
16+
* 如何使用花括號在 JSX 中使用 JavaScript 對象
1717

1818
</YouWillLearn>
1919

20-
## Passing strings with quotes {/*passing-strings-with-quotes*/}
20+
## 使用引號傳遞 strings {/*passing-strings-with-quotes*/}
2121

22-
When you want to pass a string attribute to JSX, you put it in single or double quotes:
22+
當你想向 JSX 傳遞一個 string attribute 時,你要把它放在單引號或雙引號中:
2323

2424
<Sandpack>
2525

@@ -41,9 +41,9 @@ export default function Avatar() {
4141

4242
</Sandpack>
4343

44-
Here, `"https://i.imgur.com/7vQD0fPs.jpg"` and `"Gregorio Y. Zara"` are being passed as strings.
44+
在這裡, `"https://i.imgur.com/7vQD0fPs.jpg"` `"Gregorio Y. Zara"` 被作為 strings 轉遞。
4545

46-
But what if you want to dynamically specify the `src` or `alt` text? You could **use a value from JavaScript by replacing `"` and `"` with `{` and `}`**:
46+
但是如果你想動態地指定 `src` `alt` 文本呢?**你可以替換`"``"``{``}`,以使用 JavaScript 中的值,**
4747

4848
<Sandpack>
4949

@@ -67,11 +67,11 @@ export default function Avatar() {
6767

6868
</Sandpack>
6969

70-
Notice the difference between `className="avatar"`, which specifies an `"avatar"` CSS class name that makes the image round, and `src={avatar}` that reads the value of the JavaScript variable called `avatar`. That's because curly braces let you work with JavaScript right there in your markup!
70+
注意 `className="avatar"``src={avatar}` 之間的區別,前者指定了一個 `"avatar"` 的CSS類名,使圖片變成圓形,而後者讀取了 JavaScript 變量 `avatar` 的值。這是因為花括號可以讓你在標籤中使用 JavaScript !
7171

72-
## Using curly braces: A window into the JavaScript world {/*using-curly-braces-a-window-into-the-javascript-world*/}
72+
## 使用花括號: 進入 JavaScript 世界的窗口 {/*using-curly-braces-a-window-into-the-javascript-world*/}
7373

74-
JSX is a special way of writing JavaScript. That means it’s possible to use JavaScript inside it—with curly braces `{ }`. The example below first declares a name for the scientist, `name`, then embeds it with curly braces inside the `<h1>`:
74+
JSX 是編寫 JavaScript 的一種特殊方式。這意味著可以在它裡面透過大括號 `{ }` 使用 JavaScript 。下面的例子首先聲明了一位科學家的名字 `name` ,然後用大括號將其嵌入 `<h1>` 內:
7575

7676
<Sandpack>
7777

@@ -86,9 +86,9 @@ export default function TodoList() {
8686
8787
</Sandpack>
8888
89-
Try changing `name`'s value from `'Gregorio Y. Zara'` to `'Hedy Lamarr'`. See how the To Do List title changes?
89+
試著把 `name` 的值從 `Gregorio Y. Zara` 改為 `Hedy Lamarr` 。看到待辦事項列表的標題如何變化了嗎?
9090
91-
Any JavaScript expression will work between curly braces, including function calls like `formatDate()`:
91+
任何的 JavaScript 表達式都可以在花括號之間使用,包括像 `formatDate()` 這樣的函數調用:
9292
9393
<Sandpack>
9494
@@ -111,18 +111,18 @@ export default function TodoList() {
111111
112112
</Sandpack>
113113
114-
### Where to use curly braces {/*where-to-use-curly-braces*/}
114+
### 哪裡使用花括號 {/*where-to-use-curly-braces*/}
115115
116-
You can only use curly braces in two ways inside JSX:
116+
你只能在 JSX 中以兩種方式使用花括號。
117117
118-
1. **As text** directly inside a JSX tag: `<h1>{name}'s To Do List</h1>` works, but `<{tag}>Gregorio Y. Zara's To Do List</{tag}>` will not.
119-
2. **As attributes** immediately following the `=` sign: `src={avatar}` will read the `avatar` variable, but `src="{avatar}"` will pass the string `"{avatar}"`.
118+
1. **作為文本**直接在JSX標籤內使用。 `<h1>{name}'s To Do List</h1>` 可以使用,但 `<{tag}>Gregorio Y. Zara's To Do List</{tag}>` 就不行。
119+
2. **作為 attributes** 緊隨 `=` 符號之後:`src={avatar}` 將讀取 `avatar` 變量,但 `src="{avatar}"` 將傳遞 string `"{avatar}"`
120120
121-
## Using "double curlies": CSS and other objects in JSX {/*using-double-curlies-css-and-other-objects-in-jsx*/}
121+
## 使用 "雙花括號":JSX 中的 CSS 和其他對象 {/*using-double-curlies-css-and-other-objects-in-jsx*/}
122122
123-
In addition to strings, numbers, and other JavaScript expressions, you can even pass objects in JSX. Objects are also denoted with curly braces, like `{ name: "Hedy Lamarr", inventions: 5 }`. Therefore, to pass a JS object in JSX, you must wrap the object in another pair of curly braces: `person={{ name: "Hedy Lamarr", inventions: 5 }}`.
123+
除了 stringsnumbers 和其他 JavaScript 表達式外,你甚至可以在 JSX 中傳遞對象。對像也用花括號表示,如 `{ name: "Hedy Lamarr", inventions: 5 }` 。因此,要在JSX中傳遞一個 JS 對象,你必須用另一對花括號包裹該對象:`person={{ name: "Hedy Lamarr", inventions: 5 }}`
124124
125-
You may see this with inline CSS styles in JSX. React does not require you to use inline styles (CSS classes work great for most cases). But when you need an inline style, you pass an object to the `style` attribute:
125+
你可能會在 JSX 中看到這種內聯 CSS 樣式。 React 並不要求你使用內聯樣式(CSS類在大多數情況下效果很好)。但是當你需要一個內聯樣式時,你要向 `style` attribute 傳遞一個對象:
126126
127127
<Sandpack>
128128
@@ -148,9 +148,9 @@ ul { padding: 20px 20px 20px 40px; margin: 0; }
148148
149149
</Sandpack>
150150
151-
Try changing the values of `backgroundColor` and `color`.
151+
嘗試改變 `backgroundColor` `color` 的值。
152152
153-
You can really see the JavaScript object inside the curly braces when you write it like this:
153+
當你這樣寫時,你清楚地看到花括號內的 JavaScript 對象。
154154
155155
```js {2-5}
156156
<ul style={
@@ -161,17 +161,17 @@ You can really see the JavaScript object inside the curly braces when you write
161161
}>
162162
```
163163
164-
The next time you see `{{` and `}}` in JSX, know that it's nothing more than an object inside the JSX curlies!
164+
下次你在JSX中看到 `{{` `}}` 的時候,要知道它只不過是 JSX 花括號中的一個對象而已!
165165
166166
<Pitfall>
167167
168-
Inline `style` properties are written in camelCase. For example, HTML `<ul style="background-color: black">` would be written as `<ul style={{ backgroundColor: 'black' }}>` in your component.
168+
內聯的 `style` 屬性是用 camelCase 寫的。例如,HTML `<ul style="background-color: black">` 在你的 component 中應該寫成 `<ul style={{ backgroundColor: 'black' }}>`
169169
170170
</Pitfall>
171171
172-
## More fun with JavaScript objects and curly braces {/*more-fun-with-javascript-objects-and-curly-braces*/}
172+
## 更多關於 JavaScript 對象和花括號的樂趣 {/*more-fun-with-javascript-objects-and-curly-braces*/}
173173
174-
You can move several expressions into one object, and reference them in your JSX inside curly braces:
174+
你可以把幾個表達式移到一個對像中,並在你的 JSX 中使用花括號引用它們。
175175
176176
<Sandpack>
177177
@@ -211,7 +211,7 @@ body > div > div { padding: 20px; }
211211

212212
</Sandpack>
213213

214-
In this example, the `person` JavaScript object contains a `name` string and a `theme` object:
214+
在這個例子中,`person` JavaScript 對象包含一個 `name` string 和一個 `theme` 對象。
215215

216216
```js
217217
const person = {
@@ -223,31 +223,31 @@ const person = {
223223
};
224224
```
225225

226-
The component can use these values from `person` like so:
226+
該組件可以像這樣使用這些來自 `person` 的值。
227227

228228
```js
229229
<div style={person.theme}>
230230
<h1>{person.name}'s Todos</h1>
231231
```
232232
233-
JSX is very minimal as a templating language because it lets you organize data and logic using JavaScript.
233+
JSX 是非常簡潔的模板語言,因為它允許使用 JavaScript 來組織數據和邏輯。
234234
235235
<Recap>
236236
237-
Now you know almost everything about JSX:
237+
現在你已經幾乎了解 JSX 的所有內容:
238238
239-
* JSX attributes inside quotes are passed as strings.
240-
* Curly braces let you bring JavaScript logic and variables into your markup.
241-
* They work inside the JSX tag content or immediately after `=` in attributes.
242-
* `{{` and `}}` is not special syntax: it's a JavaScript object tucked inside JSX curly braces.
239+
* 在引號內的 JSX attributes 被傳遞為字符串。
240+
* 花括號可以讓你將 JavaScript 邏輯和變量帶入標籤中。
241+
* 它們可以在 JSX 標籤內容內部或在屬性中的 = 後立即使用。
242+
* `{{` `}}` 不是特殊的語法:它是一個被包含在 JSX 花括號內的 JavaScript 對象。
243243
244244
</Recap>
245245
246246
<Challenges>
247247
248-
#### Fix the mistake {/*fix-the-mistake*/}
248+
#### 糾正錯誤 {/*fix-the-mistake*/}
249249
250-
This code crashes with an error saying `Objects are not valid as a React child`:
250+
這段代碼會崩潰並報錯 `Objects are not valid as a React child`:
251251
252252
<Sandpack>
253253
@@ -287,15 +287,15 @@ body > div > div { padding: 20px; }
287287

288288
</Sandpack>
289289

290-
Can you find the problem?
290+
你能找到問題所在嗎?
291291

292-
<Hint>Look for what's inside the curly braces. Are we putting the right thing there?</Hint>
292+
<Hint>看一下花括號裡的內容。我們是否把正確的東西放在那裡? </Hint>
293293

294294
<Solution>
295295

296-
This is happening because this example renders *an object itself* into the markup rather than a string: `<h1>{person}'s Todos</h1>` is trying to render the entire `person` object! Including raw objects as text content throws an error because React doesn't know how you want to display them.
296+
發生這種情況是因為這個例子將*一個對象本身*渲染到標籤中,而不是一個字符串。 `<h1>{person}'s Todos</h1>` 將試圖渲染整個 `person` 對象! 使用原始對像作為文本內容會拋出一個錯誤,因為React不知道你想如何顯示它們。
297297

298-
To fix it, replace `<h1>{person}'s Todos</h1>` with `<h1>{person.name}'s Todos</h1>`:
298+
要解決這個問題,將 `<h1>{person}'s Todos</h1>` 改為 `<h1>{person.name}'s Todos</h1>`
299299

300300
<Sandpack>
301301

@@ -337,9 +337,9 @@ body > div > div { padding: 20px; }
337337
338338
</Solution>
339339
340-
#### Extract information into an object {/*extract-information-into-an-object*/}
340+
#### 將信息提取到一個對像中 {/*extract-information-into-an-object*/}
341341
342-
Extract the image URL into the `person` object.
342+
將圖片的 URL 提取到 `person` 對像中。
343343
344344
<Sandpack>
345345
@@ -381,7 +381,7 @@ body > div > div { padding: 20px; }
381381

382382
<Solution>
383383

384-
Move the image URL into a property called `person.imageUrl` and read it from the `<img>` tag using the curlies:
384+
將圖片 URL 移到一個名為 `person.imageUrl` 的屬性中,然後在 `<img>` 標籤中使用花括號讀取它:
385385

386386
<Sandpack>
387387

@@ -424,13 +424,13 @@ body > div > div { padding: 20px; }
424424
425425
</Solution>
426426
427-
#### Write an expression inside JSX curly braces {/*write-an-expression-inside-jsx-curly-braces*/}
427+
#### JSX 的花括號中寫表達式 {/*write-an-expression-inside-jsx-curly-braces*/}
428428
429-
In the object below, the full image URL is split into four parts: base URL, `imageId`, `imageSize`, and file extension.
429+
在下面的對像中,完整的圖片 URL 被分成四個部分:base URL`imageId``imageSize` 和文件擴展名。
430430
431-
We want the image URL to combine these attributes together: base URL (always `'https://i.imgur.com/'`), `imageId` (`'7vQD0fP'`), `imageSize` (`'s'`), and file extension (always `'.jpg'`). However, something is wrong with how the `<img>` tag specifies its `src`.
431+
我們希望將這些 attributes 組合在一起形成完整的圖片 URL:基礎 URL(始終為 `https://i.imgur.com/`)、`imageId`(`7vQD0fP`)、`imageSize`(`s`)和文件擴展名(始終為 `.jpg`)。然而,下面 `<img>` 標籤的 src 指定方式存在問題。
432432
433-
Can you fix it?
433+
你能修復它嗎?
434434
435435
<Sandpack>
436436
@@ -474,15 +474,15 @@ body > div > div { padding: 20px; }
474474
475475
</Sandpack>
476476
477-
To check that your fix worked, try changing the value of `imageSize` to `'b'`. The image should resize after your edit.
477+
要檢查你的修復是否生效,請嘗試將 `imageSize` 的值更改為 `'b'`。在你的編輯後,該圖像應該會改變大小。
478478
479479
<Solution>
480480
481-
You can write it as `src={baseUrl + person.imageId + person.imageSize + '.jpg'}`.
481+
你可以將其寫成 `src={baseUrl + person.imageId + person.imageSize + '.jpg'}`
482482
483-
1. `{` opens the JavaScript expression
484-
2. `baseUrl + person.imageId + person.imageSize + '.jpg'` produces the correct URL string
485-
3. `}` closes the JavaScript expression
483+
1. `{` 開始 JavaScript 表達式
484+
2. `baseUrl + person.imageId + person.imageSize + '.jpg'` 生成正確的 URL string
485+
3. `}` 結束 JavaScript 表達式
486486
487487
<Sandpack>
488488
@@ -525,7 +525,7 @@ body > div > div { padding: 20px; }
525525
526526
</Sandpack>
527527
528-
You can also move this expression into a separate function like `getImageUrl` below:
528+
你也可以將這個表達式移到一個單獨的函數中,如下面的 `getImageUrl` 函數:
529529
530530
<Sandpack>
531531
@@ -580,7 +580,7 @@ body > div > div { padding: 20px; }
580580
581581
</Sandpack>
582582
583-
Variables and functions can help you keep the markup simple!
583+
變量和函數可以幫助你保持標籤簡潔!
584584
585585
</Solution>
586586

0 commit comments

Comments
 (0)