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/javascript-in-jsx-with-curly-braces.md
+56-56
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:
@@ -148,9 +148,9 @@ ul { padding: 20px 20px 20px 40px; margin: 0; }
148
148
149
149
</Sandpack>
150
150
151
-
Try changing the values of `backgroundColor`and`color`.
151
+
尝试更改 `backgroundColor` 和 `color` 的值。
152
152
153
-
You can really see the JavaScript object inside the curly braces when you write it like this:
153
+
当你像下面的例子那样写时,你真的可以发现花括号内的对象:
154
154
155
155
```js {2-5}
156
156
<ul style={
@@ -161,17 +161,17 @@ You can really see the JavaScript object inside the curly braces when you write
161
161
}>
162
162
```
163
163
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 花括号里的一个对象!
165
165
166
166
<Gotcha>
167
167
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.
The component can use these values from `person` like so:
226
+
该组件可以这样使用来自 `person`的值:
227
227
228
228
```js
229
229
<div style={person.theme}>
230
230
<h1>{person.name}'s Todos</h1>
231
231
```
232
232
233
-
JSX is very minimal as a templating language because it lets you organize data and logic using JavaScript.
233
+
JSX 是一个十分简洁的模板语言,因为它允许你使用 JavaScript 组织数据和逻辑。
234
234
235
235
<Recap>
236
236
237
-
Now you know almost everything about JSX:
237
+
现在你几乎了解了 JSX 的一切:
238
238
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 引号内的值作为字符串传递给属性。
240
+
* 花括号让你可以将 JavaScript 的逻辑和变量带入你的标记中。
241
+
* 它们在 JSX 标签中的内容或紧随属性后的 `=` 有效。
242
+
* `{{` 和 `}}` 不是特殊语法:它是在 JSX 花括号内的 JavaScript 对象。
243
243
244
244
</Recap>
245
245
246
246
<Challenges>
247
247
248
-
### Fix the mistake {/*fix-the-mistake*/}
248
+
### 修复错误 {/*fix-the-mistake*/}
249
249
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`:
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