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/content/learn/javascript-in-jsx-with-curly-braces.md
+56-56Lines changed: 56 additions & 56 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,25 +1,25 @@
1
1
---
2
-
title: JavaScript in JSX with Curly Braces
2
+
title: 在 JSX 中使用 JavaScript 的語法
3
3
---
4
4
5
5
<Intro>
6
6
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.
@@ -67,11 +67,11 @@ export default function Avatar() {
67
67
68
68
</Sandpack>
69
69
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!
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>`:
@@ -111,18 +111,18 @@ export default function TodoList() {
111
111
112
112
</Sandpack>
113
113
114
-
### Where to use curly braces {/*where-to-use-curly-braces*/}
114
+
### 哪裡使用大括號 {/*where-to-use-curly-braces*/}
115
115
116
-
You can only use curly braces in two ways inside JSX:
116
+
你只能在 JSX 中以兩種方式使用大括號。
117
117
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}>` 就不行。
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 }}`.
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:
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.
This code crashes with an error saying `Objects are not valid as a React child`:
250
+
這段代碼會崩潰並報錯 `Objects are not valid as a React child`:
251
251
252
252
<Sandpack>
253
253
@@ -287,15 +287,15 @@ body > div > div { padding: 20px; }
287
287
288
288
</Sandpack>
289
289
290
-
Can you find the problem?
290
+
你能找到問題所在嗎?
291
291
292
-
<Hint>Look for what's inside the curly braces. Are we putting the right thing there?</Hint>
292
+
<Hint>看一下大括號裡的內容。我們是否把正確的東西放在那裡? </Hint>
293
293
294
294
<Solution>
295
295
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.
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`.
0 commit comments