From 739f9492e961a5ad8d532d86ccf04cc3c13f072d Mon Sep 17 00:00:00 2001 From: flynn-eye <939649107@qq.com> Date: Sat, 20 Nov 2021 22:05:53 +0800 Subject: [PATCH 1/5] docs(cn): learn/javascript-in-jsx-with-curly-braces --- .../javascript-in-jsx-with-curly-braces.md | 112 +++++++++--------- 1 file changed, 56 insertions(+), 56 deletions(-) diff --git a/beta/src/pages/learn/javascript-in-jsx-with-curly-braces.md b/beta/src/pages/learn/javascript-in-jsx-with-curly-braces.md index 6572c23fad..7c75ea21fb 100644 --- a/beta/src/pages/learn/javascript-in-jsx-with-curly-braces.md +++ b/beta/src/pages/learn/javascript-in-jsx-with-curly-braces.md @@ -1,25 +1,25 @@ --- -title: JavaScript in JSX with Curly Braces +title: 在 JSX 的花括号内使用 JavaScript --- -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. +JSX 允许你在 JavaScript 中编写类似 HTML 的标记,让渲染逻辑和内容在同一处地方。有些情况下,你想在一处标记中添加一些 JavaScript 逻辑或者引用动态属性。这种情况下,你可以在 JSX 的花括号内来编写 JavaScript。 -* How to pass strings with quotes -* How to reference a JavaScript variable inside JSX with curly braces -* How to call a JavaScript function inside JSX with curly braces -* How to use a JavaScript object inside JSX with curly braces +* 如何使用引号传递字符串 +* 在 JSX 的花括号内引用 JavaScript 变量 +* 在 JSX 的花括号内调用 JavaScript 函数 +* 在 JSX 的花括号内使用 JavaScript 对象 -## Passing strings with quotes {/*passing-strings-with-quotes*/} +## 使用引号传递字符串 {/*passing-strings-with-quotes*/} -When you want to pass a string attribute to JSX, you put it in single or double quotes: +当你想传递一个字符串属性给 JSX 时,把它放到单引号或双引号中: @@ -41,9 +41,9 @@ export default function Avatar() { -Here, `"https://i.imgur.com/7vQD0fPs.jpg"` and `"Gregorio Y. Zara"` are being passed as strings. +在此处,`"https://i.imgur.com/7vQD0fPs.jpg"` 和 `"Gregorio Y. Zara"` 作为字符串被传递。 -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 `}`**: +但是如果你想动态指定 `src` 或 `alt` 该怎么办?你可以**使用 `{` 和 `}` 替代 `"` 和 `"` 来引用 JavaScript 的一个值**: @@ -67,11 +67,11 @@ export default function Avatar() { -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! +注意 `className="avatar"` 和 `src={avatar}` 之间的区别,`className="avatar"` 声明了一个叫 `"avatar"` 的 CSS 类名让图片变圆,而 `src={avatar}` 读取了 JavaScript 中一个叫做 `avatar` 的变量的值。正是因为花括号让 JavaScript 可以在你的标记中生效! -## Using curly braces: A window into the JavaScript world {/*using-curly-braces-a-window-into-the-javascript-world*/} +## 使用花括号:一扇进入 JavaScript 世界的窗户 {/*using-curly-braces-a-window-into-the-javascript-world*/} -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 `

`: +JSX 是一种编写 JavaScript 的特殊方式。它意味着在花括号 `{ }` 中使用 JavaScript 成为可能。下面的例子中声明了科学家的名字,`name`,然后在 `

` 后的花括号内嵌入它: @@ -86,9 +86,9 @@ export default function TodoList() { -Try changing `name`'s value from `'Gregorio Y. Zara'` to `'Hedy Lamarr'`. See how the To Do List title changes? +尝试将 `name` 的值从 `'Gregorio Y. Zara'` 更改成 `'Hedy Lamarr'`。看看这个 To Do List 的标题如何变化? -Any JavaScript expression will work between curly braces, including function calls like `formatDate()`: +任何花括号内的 JavaScript 表达式都会工作,包括 像 `formatDate()` 这样的函数调用: @@ -111,18 +111,18 @@ export default function TodoList() { -### Where to use curly braces {/*where-to-use-curly-braces*/} +### 可以在哪使用花括号 {/*where-to-use-curly-braces*/} -You can only use curly braces in two ways inside JSX: +在 JSX 中只能以下面两种方式使用花括号: -1. **As text** directly inside a JSX tag: `

{name}'s To Do List

` works, but `<{tag}>Gregorio Y. Zara's To Do List` will not. -2. **As attributes** immediately following the `=` sign: `src={avatar}` will read the `avatar` variable, but `src="{avatar}"` will pass the string `{avatar}`. +1. 用作 JSX 标签内的**文本**:`

{name}'s To Do List

` 是有效的,但是 `<{tag}>Gregorio Y. Zara's To Do List` 无效。 +2. 用作紧跟在 `=` 符号后的**属性**:`src={avatar}` 会读取 `avatar` 的值,但是 `src="{avatar}"` 会传一个叫做 `{avatar}` 的字符串的值。 -## Using "double curlies": CSS and other objects in JSX {/*using-double-curlies-css-and-other-objects-in-jsx*/} +## 使用 "双花括号":JSX 中的 CSS 和 对象 {/*using-double-curlies-css-and-other-objects-in-jsx*/} -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 }}`. +除了字符串、数字和其它 JavaScript 表达式,你甚至可以在 JSX 中传递对象。对象也用花括号表示,例如 `{ name: "Hedy Lamarr", inventions: 5 }`。因此,为了在 JSX 中传递对象,你必须用另一对花括号包裹对象:`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: +你也许看到了内联在 JSX 中的 CSS 样式。React 不强制你使用内联样式(CSS 类适用于大多数情况)。但是当你需要内联样式的时候,你可以传递一个对象给 `style` 属性: @@ -148,9 +148,9 @@ ul { padding: 20px 20px 20px 40px; margin: 0; } -Try changing the values of `backgroundColor` and `color`. +尝试更改 `backgroundColor` 和 `color` 的值。 -You can really see the JavaScript object inside the curly braces when you write it like this: +当你像下面的例子那样写时,你真的可以发现花括号内的对象: ```js {2-5}