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: src/content/learn/javascript-in-jsx-with-curly-braces.md
+60-56Lines changed: 60 additions & 56 deletions
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: JavaScript di JSX Menggunakan Kurung Kurawal
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 memungkinkan Anda menulis markup mirip HTML di dalam file JavaScript, sehingga membuat logika rendering dan konten berada pada satu tempat yang sama. Terkadang Anda akan ingin menambahkan sedikit logika JavaScript atau merujuk pada properti yang dinamis di dalam markup tersebut. Dalam situasi ini, Anda dapat menggunakan tanda kurung kurawal pada JSX untuk membuka akses ke 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
+
*Bagaimana cara mengoper *string* dengan tanda kutip
14
+
*Bagaimana cara mereferensikan variabel didalam JSX dengan kurung kurawal
15
+
*Bagaimana cara memanggil fungsi Javascript didalam JSX dengan kurung kurawal
16
+
*Bagaimana cara menggunakan objek Javascript didalam JSX dengan kurung kurawal
17
17
18
18
</YouWillLearn>
19
19
20
-
## Passing strings with quotes {/*passing-strings-with-quotes*/}
20
+
## Mengoper string dengan tanda kutip {/*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
+
Ketika Anda ingin oper atribut *string* ke JSX, Anda memasukkannya ke dalam tanda kutip tunggal atau ganda:
23
23
24
24
<Sandpack>
25
25
@@ -41,9 +41,10 @@ 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
+
Di sini, `"https://i.imgur.com/7vQD0fPs.jpg"` dan `"Gregorio Y. Zara"` sedang dioper sebagai *string*.
45
+
46
+
Namun bagaimana jika Anda ingin secara dinamis menentukan teks `src` atau `alt? Anda dapat **menggunakan nilai dari JavaScript dengan mengganti `"` dan `"` dengan `{` dan `}`**:
45
47
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 `}`**:
47
48
48
49
<Sandpack>
49
50
@@ -67,11 +68,11 @@ export default function Avatar() {
67
68
68
69
</Sandpack>
69
70
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!
71
+
Perhatikan perbedaan antara `className="avatar"`, yang menentukan nama kelas CSS `"avatar"`yang membuat gambar bulat, dan`src={avatar}`yang membaca nilai variabel JavaScript disebut `avatar`. Hal itu terjadi karena kurung kurawal memungkinkan Anda bekerja dengan JavaScript langsung di markup Anda!
71
72
72
-
## Using curly braces: A window into the JavaScript world {/*using-curly-braces-a-window-into-the-javascript-world*/}
73
+
## Menggunakan kurung kurawal: Jendela ke dunia JavaScript {/*using-curly-braces-a-window-into-the-javascript-world*/}
73
74
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>`:
75
+
JSX merupakan cara khusus dalam menulis JavaScript. Artinya, memungkinkan untuk menggunakan JavaScript di dalamnya - dengan kurung kurawal`{ }`. Contohnya di bawah ini pertama-tama mendeklarasikan sebuah nama untuk ilmuwan, `name`, kemudian menyematkannya dengan kurung kurawal di dalam`<h1>`:
75
76
76
77
<Sandpack>
77
78
@@ -86,9 +87,9 @@ export default function TodoList() {
86
87
87
88
</Sandpack>
88
89
89
-
Try changing the `name`'s value from `'Gregorio Y. Zara'`to`'Hedy Lamarr'`. See how the list title changes?
90
+
Coba ubah nilai `name` dari `'Gregorio Y. Zara'` menjadi `'Hedy Lamarr'`. Lihat bagaimana judul daftar berubah?
90
91
91
-
Any JavaScript expression will work between curly braces, including function calls like `formatDate()`:
92
+
Setiap ekspresi JavaScript akan berfungsi di antara kurung kurawal, termasuk fungsi seperti `formatDate()`:
92
93
93
94
<Sandpack>
94
95
@@ -111,18 +112,18 @@ export default function TodoList() {
111
112
112
113
</Sandpack>
113
114
114
-
### Where to use curly braces {/*where-to-use-curly-braces*/}
115
+
### Dimana menggunakan kurung kurawal? {/*where-to-use-curly-braces*/}
115
116
116
-
You can only use curly braces in two ways inside JSX:
117
+
Anda hanya dapat menggunakan kurung kurawal (curly braces) dalam dua cara di dalam JSX:
117
118
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}"`.
119
+
1. **Sebagai teks** langsung di dalam tag JSX: `<h1>Daftar Tugas {name}</h1>` berfungsi, tetapi `<{tag}>Daftar Tugas Gregorio Y. Zara</{tag}>` tidak akan berhasil.
120
+
2. **Sebagai atribut** yang segera mengikuti tanda dengan `=`: `src={avatar}` akan membaca variabel `avatar`, tetapi `src="{avatar}"` akan mengoper string `"{avatar}"`.
120
121
121
-
## Using "double curlies": CSS and other objects in JSX {/*using-double-curlies-css-and-other-objects-in-jsx*/}
122
+
## Menggunakan "kurung kurawal ganda": CSS dan objek lain di JSX {/*using-double-curlies-css-and-other-objects-in-jsx*/}
122
123
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 }}`.
124
+
Selain string, angka, dan ekspresi JavaScript lainnya, Anda bahkan dapat mengoper objek dalam JSX. Objek juga ditandai dengan kurung kurawal, seperti `{ name: "Hedy Lamarr", inventions: 5 }`. Oleh karena itu, untuk mengoper objek JavaScript di JSX, Anda harus membungkus objek tersebut dalam sepasang kurung kurawal lainnya: `person={{ name: "Hedy Lamarr", inventions: 5 }}`.
124
125
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:
126
+
Anda mungkin melihat ini pada gaya CSS *inline* dalam JSX. React tidak mengharuskan Anda untuk menggunakan gaya *inline* (kelas CSS berfungsi lebih baik untuk kebanyakan kasus). Namun, ketika Anda membutuhkan gaya *inline*, Anda dapat mengoper objek ke atribut `style`:
126
127
127
128
<Sandpack>
128
129
@@ -148,9 +149,10 @@ ul { padding: 20px 20px 20px 40px; margin: 0; }
148
149
149
150
</Sandpack>
150
151
151
-
Try changing the values of `backgroundColor` and `color`.
152
+
Coba ubah nilai dari `backgroundColor` dan `color`.
153
+
154
+
Anda dapat melihat objek JavaScript di dalam kurung kurawal dengan jelas ketika Anda menulisnya seperti ini:
152
155
153
-
You can really see the JavaScript object inside the curly braces when you write it like this:
154
156
155
157
```js {2-5}
156
158
<ul style={
@@ -161,17 +163,18 @@ You can really see the JavaScript object inside the curly braces when you write
161
163
}>
162
164
```
163
165
164
-
The next time you see `{{` and `}}` in JSX, know that it's nothing more than an object inside the JSX curlies!
166
+
Ketika Anda melihat `{{` dan `}}` di dalam JSX, Anda akan tahu bahwa itu tidak lebih dari objek di dalam kurung kurawal JSX!
167
+
165
168
166
169
<Pitfall>
167
170
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.
171
+
Properti `style` dalam bentuk *inline* ditulis menggunakan gaya *camelCase* di dalam JSX. Sebagai contoh, jika pada HTML Anda menuliskan `<ul style="background-color: black">`, maka pada komponen React Anda harus menulisnya sebagai `<ul style={{ backgroundColor: 'black' }}>`.
169
172
170
173
</Pitfall>
171
174
172
-
## More fun with JavaScript objects and curly braces {/*more-fun-with-javascript-objects-and-curly-braces*/}
175
+
## Lebih banyak kesenangan dengan objek JavaScript dan kurung kurawal {/*more-fun-with-javascript-objects-and-curly-braces*/}
173
176
174
-
You can move several expressions into one object, and reference them in your JSX inside curly braces:
177
+
Anda dapat memasukkan beberapa ekspresi ke dalam satu objek, dan merujuk pada objek tersebut di dalam JSX menggunakan kurung kurawal. Sebagai contoh:
175
178
176
179
<Sandpack>
177
180
@@ -211,7 +214,7 @@ body > div > div { padding: 20px; }
211
214
212
215
</Sandpack>
213
216
214
-
In this example, the `person` JavaScript object contains a `name` string and a `theme` object:
217
+
Pada contoh ini, objek JavaScript `person`berisi sebuah string `name`dan sebuah objek`theme`:
215
218
216
219
```js
217
220
constperson= {
@@ -223,31 +226,32 @@ const person = {
223
226
};
224
227
```
225
228
226
-
The component can use these values from `person` like so:
229
+
Komponen dapat menggunakan nilai-nilai dari objek `person` seperti ini:
230
+
227
231
228
232
```js
229
233
<div style={person.theme}>
230
234
<h1>{person.name}'s Todos</h1>
231
235
```
232
236
233
-
JSX is very minimal as a templating language because it lets you organize data and logic using JavaScript.
237
+
JSX sangat sederhana sebagai bahasa templating karena memungkinkan Anda untuk mengorganisir data dan logika menggunakan JavaScript.
234
238
235
239
<Recap>
236
240
237
-
Now you know almost everything about JSX:
241
+
Sekarang Anda hampir tahu segalanya tentang JSX:
238
242
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.
243
+
* Atribut JSX di dalam tanda kutip dianggap sebagai string.
244
+
* Kurung kurawal memungkinkan Anda membawa logika JavaScript dan variabel ke dalam markup.
245
+
* Kurung kurawal berfungsi di dalam konten tag JSX atau segera setelah `=` pada atribut.
246
+
* `{{` dan `}}` bukanlah sintaks khusus: itu adalah objek JavaScript yang tersembunyi di dalam kurung kurawal JSX.
243
247
244
248
</Recap>
245
249
246
250
<Challenges>
247
251
248
-
#### Fix the mistake {/*fix-the-mistake*/}
252
+
#### Perbaiki Kesalahan {/*fix-the-mistake*/}
249
253
250
-
This code crashes with an error saying`Objects are not valid as a React child`:
254
+
Kode ini mengalami *crash* dengan pesan galat yang menyatakan `Objects are not valid as a React child`:
251
255
252
256
<Sandpack>
253
257
@@ -287,15 +291,15 @@ body > div > div { padding: 20px; }
287
291
288
292
</Sandpack>
289
293
290
-
Can you find the problem?
294
+
Bisakah Anda menemukan masalahnya?
291
295
292
-
<Hint>Look for what's inside the curly braces. Are we putting the right thing there?</Hint>
296
+
<Hint>Periksa apa yang ada di dalam kurung kurawal. Apakah kita meletakkan sesuatu yang benar di sana?</Hint>
293
297
294
298
<Solution>
295
299
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.
300
+
Ini terjadi karena contoh ini me-*render**objek itu sendiri* ke dalam markup bukan string: `<h1>{person}'s Todos</h1>`mencoba *render* seluruh objek`person`! Menyertakan objek mentah sebagai konten teks menghasilkan kesalahan karena React tidak tahu bagaimana Anda ingin menampilkannya.
297
301
298
-
To fix it, replace `<h1>{person}'s Todos</h1>`with`<h1>{person.name}'s Todos</h1>`:
302
+
Untuk memperbaikinya, ganti`<h1>{person}'s Todos</h1>`dengan`<h1>{person.name}'s Todos</h1>`:
299
303
300
304
<Sandpack>
301
305
@@ -337,9 +341,9 @@ body > div > div { padding: 20px; }
337
341
338
342
</Solution>
339
343
340
-
#### Extract information into an object {/*extract-information-into-an-object*/}
344
+
#### Ekstrak informasi ke dalam sebuah objek {/*extract-information-into-an-object*/}
341
345
342
-
Extract the image URL into the`person` object.
346
+
Ekstrak URL gambar ke dalam objek `person`.
343
347
344
348
<Sandpack>
345
349
@@ -381,7 +385,7 @@ body > div > div { padding: 20px; }
381
385
382
386
<Solution>
383
387
384
-
Move the image URL into a property called `person.imageUrl` and read it from the `<img>` tag using the curlies:
388
+
Pindahkan URL gambar ke dalam properti yang disebut`person.imageUrl`dan baca dari tag `<img>`menggunakan kurung kurawal:
385
389
386
390
<Sandpack>
387
391
@@ -424,13 +428,13 @@ body > div > div { padding: 20px; }
424
428
425
429
</Solution>
426
430
427
-
#### Write an expression inside JSX curly braces {/*write-an-expression-inside-jsx-curly-braces*/}
431
+
#### Menulis ekspresi di dalam kurung kurawal JSX {/*write-an-expression-inside-jsx-curly-braces*/}
428
432
429
-
In the object below, the full image URL is split into four parts: base URL, `imageId`, `imageSize`, and file extension.
433
+
Pada objek di bawah ini, URL lengkap gambar dibagi menjadi empat bagian: URL dasar, `imageId`, `imageSize`, dan ekstensi file.
430
434
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`.
435
+
Kita ingin URL gambar menggabungkan atribut-atribut ini bersama-sama: URL dasar (selalu `'https://i.imgur.com/'`), `imageId` (`'7vQD0fP'`), `imageSize` (`'s'`), dan ekstensi file (selalu `'.jpg'`). Namun, ada yang salah dengan bagaimana tag `<img>` menentukan `src`.
432
436
433
-
Can you fix it?
437
+
Bisakah anda memperbaikinya?
434
438
435
439
<Sandpack>
436
440
@@ -474,15 +478,15 @@ body > div > div { padding: 20px; }
474
478
475
479
</Sandpack>
476
480
477
-
To check that your fix worked, try changing the value of `imageSize` to `'b'`. The image should resize after your edit.
481
+
Untuk memeriksa apakah perbaikan Anda berhasil, coba ubah nilai dari `imageSize`menjadi`'b'`. Gambar harus menyesuaikan ukuran setelah diubah.
478
482
479
483
<Solution>
480
484
481
-
You can write it as `src={baseUrl + person.imageId + person.imageSize + '.jpg'}`.
485
+
Anda bisa menuliskannya seperti ini`src={baseUrl + person.imageId + person.imageSize + '.jpg'}`.
0 commit comments