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.
7
+
JSX により、JavaScript ファイル内に HTML のようなマークアップを書いて、レンダリングロジックとコンテンツを同じ場所にまとめられるようになります。ときに、そのマークアップの中で JavaScript のロジックを書いたり動的なプロパティを参照したりしたくなることがあります。このような場合、JSX 内で波括弧を使うことで、JavaScript への入り口を開くことができます。
8
8
9
9
</Intro>
10
10
11
11
<YouWillLearn>
12
12
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
+
*引用符を使って文字列を渡す方法
14
+
*波括弧を使って JSX 内で JavaScript 変数を参照する方法
15
+
*波括弧を使って JSX 内で JavaScript 関数を呼び出す方法
16
+
*波括弧を使って JSX 内でオブジェクトを利用する方法
17
17
18
18
</YouWillLearn>
19
19
20
-
## Passing strings with quotes {/*passing-strings-with-quotes*/}
20
+
## 引用符で文字列を渡す {/*passing-strings-with-quotes*/}
21
21
22
-
When you want to pass a string attribute to JSX, you put it in single or double quotes:
22
+
JSX に文字列の属性を渡したい場合、それをシングルクオートかダブルクオートで囲みます:
23
23
24
24
<Sandpack>
25
25
@@ -41,9 +41,9 @@ export default function Avatar() {
41
41
42
42
</Sandpack>
43
43
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"`が文字列として渡されています。
45
45
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`}`**:
@@ -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 内部で波括弧を使う方法は 2 つだけです:
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 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