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/writing-markup-with-jsx.md
+81-79Lines changed: 81 additions & 79 deletions
Original file line number
Diff line number
Diff line change
@@ -1,88 +1,89 @@
1
1
---
2
-
title: Writing Markup with JSX
2
+
title: Menulis Markup dengan JSX
3
3
---
4
4
5
5
<Intro>
6
6
7
-
*JSX* is a syntax extension for JavaScript that lets you write HTML-like markup inside a JavaScript file. Although there are other ways to write components, most React developers prefer the conciseness of JSX, and most codebases use it.
7
+
JSX adalah perpanjangan sintaksis untuk menulis kode seperti *HTML* dalam file JavaScript. Meskipun ada beberapa cara lain, JSX lebih dipilih oleh sebagian besar *developer* React dan *codebase* karena kepadatannya.
8
8
9
9
</Intro>
10
10
11
11
<YouWillLearn>
12
12
13
-
*Why React mixes markup with rendering logic
14
-
*How JSX is different from HTML
15
-
*How to display information with JSX
13
+
*Mengapa React mencampur *markup* dan logika *render*
14
+
*Perbedaan JSX dengan HTML
15
+
*Cara menampilkan informasi menggunakan JSX
16
16
17
17
</YouWillLearn>
18
18
19
-
## JSX: Putting markup into JavaScript {/*jsx-putting-markup-into-javascript*/}
19
+
## JSX: Meletakkan markup ke JavaScript {/*jsx-putting-markup-into-javascript*/}
20
20
21
-
The Web has been built on HTML, CSS, and JavaScript. For many years, web developers kept content in HTML, design in CSS, and logic in JavaScript—often in separate files! Content was marked up inside HTML while the page's logic lived separately in JavaScript:
21
+
Website selama ini terbuat dari HTML, CSS, dan JavaScript. Selama bertahun-tahun, pengembang *website* menaruh konten di HTML, desain di CSS, dan logika di JavaScript—tidak jarang di *file* yang berbeda! Konten dibangun di HTML sedangkan logika halaman disimpan secara terpisah dalam JavaScript:
22
22
23
23
<DiagramGroup>
24
24
25
-
<Diagramname="writing_jsx_html"height={237}width={325}alt="HTML markup with purple background and a div with two child tags: p and form. ">
25
+
<Diagramname="writing_jsx_html"height={237}width={325}alt="markup HTML dengan latar belakang berwarna ungu dan sebuah div dengan dua children tag: p dan form. ">
26
26
27
27
HTML
28
28
29
29
</Diagram>
30
30
31
-
<Diagramname="writing_jsx_js"height={237}width={325}alt="Three JavaScript handlers with yellow background: onSubmit, onLogin, and onClick.">
31
+
<Diagramname="writing_jsx_js"height={237}width={325}alt="Tiga fungsi JavaScript dengan latar belakang kuning: onSubmit, onLogin, and onClick.">
32
32
33
33
JavaScript
34
34
35
35
</Diagram>
36
36
37
37
</DiagramGroup>
38
38
39
-
But as the Web became more interactive, logic increasingly determined content. JavaScript was in charge of the HTML! This is why **in React, rendering logic and markup live together in the same place—components.**
39
+
Namun, seiring dengan situs yang makin iteraktif, logika semakin menentukan konten. JavaScript mengatur HTML! Inilah mengapa **dalam React, logika *render* dan *markup* berada di satu tempat yang sama—komponen.**
40
40
41
41
<DiagramGroup>
42
42
43
-
<Diagramname="writing_jsx_sidebar"height={330}width={325}alt="React component with HTML and JavaScript from previous examples mixed. Function name is Sidebar which calls the function isLoggedIn, highlighted in yellow. Nested inside the function highlighted in purple is the p tag from before, and a Form tag referencing the component shown in the next diagram.">
43
+
<Diagramname="writing_jsx_sidebar"height={330}width={325}alt="Komponen React dengan HTML dan JavaScript digabung dari contoh sebelumnya. Fungsi bernama Sidebar yang memanggil fungsi isLoggedIn, berwarna kuning. Tag p dari sebelumnya bersarang dalam fungsi berwarna ungu, dan sebuah tag Form mereferensi ke komponen yang ada di diagram berikutnya.">
44
44
45
-
`Sidebar.js` React component
45
+
Komponen React `Sidebar.js`
46
46
47
47
</Diagram>
48
48
49
-
<Diagramname="writing_jsx_form"height={330}width={325}alt="React component with HTML and JavaScript from previous examples mixed. Function name is Form containing two handlers onClick and onSubmit highlighted in yellow. Following the handlers is HTML highlighted in purple. The HTML contains a form element with a nested input element, each with an onClick prop.">
49
+
<Diagramname="writing_jsx_form"height={330}width={325}alt="Komponen React dengan HTML dan JavaScript dari contoh sebelumnya digabung. Fungsi bernama Form memiliki dua handler onClick dan onSubmit berwarna kuning. Setelah itu diikuti oleh HTML berwarna ungu. HTML tersebut memiliki elemen form dan elemen input di dalamnya, masing-masing dengan prop onClick.">
50
50
51
-
`Form.js` React component
51
+
Komponen React `Form.js`
52
52
53
53
</Diagram>
54
54
55
55
</DiagramGroup>
56
56
57
-
Keeping a button's rendering logic and markup together ensures that they stay in sync with each other on every edit. Conversely, details that are unrelated, such as the button's markup and a sidebar's markup, are isolated from each other, making it safer to change either of them on their own.
57
+
Menggabungkan logika *render* dan *markup* untuk sebuah tombol memastikan mereka tersinkronasi dengan satu sama lain pada tiap perubahan. Sebaliknya, detil yang tidak berhubungan, *markup* untuk tombol dan *sidebar*, juga tidak terhubung dengan satu sama lain, membuat perubahan masing-masing menjadi lebih aman.
58
58
59
-
Each React component is a JavaScript function that may contain some markup that React renders into the browser. React components use a syntax extension called JSX to represent that markup. JSX looks a lot like HTML, but it is a bit stricter and can display dynamic information. The best way to understand this is to convert some HTML markup to JSX markup.
59
+
Masing-masing komponen React adalah fungsi JavaScript yang bisa memiliki *markup* yang di-*render* oleh React ke peramban. Komponen React menggunakan ekstensi sitaksis yang bernama JSX untuk merepresentasikan *markup* tersebut. JSX terlihat sangat mirip dengan HTML, namun lebih ketat dan dapat menampilkan informasi secara dinamis. Cara terbaik untuk memahami JSX adalah dengan langsung mengubah beberapa *markup* HTML menjadi JSX.
60
60
61
61
<Note>
62
62
63
-
JSX and React are two separate things. They're often used together, but you *can*[use them independently](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#whats-a-jsx-transform) of each other. JSX is a syntax extension, while React is a JavaScript library.
63
+
JSX dan React adalah dua hal yang berbeda. Mereka masing-masing digunakan secara bersama, namun anda *dapat*[digunakan sendiri secara independen](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#whats-a-jsx-transform).
64
+
JSX merupakan ekstensi sintaks, sedangkan React adalah *library* JavaScript.
64
65
65
66
</Note>
66
67
67
-
## Converting HTML to JSX {/*converting-html-to-jsx*/}
68
+
## Mengubah HTML menjadi JSX {/*converting-html-to-jsx*/}
68
69
69
-
Suppose that you have some (perfectly valid) HTML:
70
+
Anggap anda memiliki HTML yang alid:
70
71
71
72
```html
72
-
<h1>Hedy Lamarr's Todos</h1>
73
+
<h1>Daftar Tugas Putri</h1>
73
74
<img
74
75
src="https://i.imgur.com/yXOvdOSs.jpg"
75
-
alt="Hedy Lamarr"
76
+
alt="Putri"
76
77
class="photo"
77
78
>
78
79
<ul>
79
-
<li>Invent new traffic lights
80
-
<li>Rehearse a movie scene
81
-
<li>Improve the spectrum technology
80
+
<li>Mengerjakan PR
81
+
<li>Pergi belanja
82
+
<li>Minum vitamin
82
83
</ul>
83
84
```
84
85
85
-
And you want to put it into your component:
86
+
Dan anda ingin meletakkannya di komponen:
86
87
87
88
```js
88
89
exportdefaultfunctionTodoList() {
@@ -92,7 +93,7 @@ export default function TodoList() {
92
93
}
93
94
```
94
95
95
-
If you copy and paste it as is, it will not work:
96
+
Jika anda salin dan tempel secara langsung, maka dia tidak akan bekerja:
96
97
97
98
98
99
<Sandpack>
@@ -101,16 +102,16 @@ If you copy and paste it as is, it will not work:
101
102
exportdefaultfunctionTodoList() {
102
103
return (
103
104
// This doesn't quite work!
104
-
<h1>Hedy Lamarr's Todos</h1>
105
+
<h1>Daftar Tugas Putri</h1>
105
106
<img
106
107
src="https://i.imgur.com/yXOvdOSs.jpg"
107
-
alt="Hedy Lamarr"
108
+
alt="Putri"
108
109
class="photo"
109
110
>
110
111
<ul>
111
-
<li>Invent new traffic lights
112
-
<li>Rehearse a movie scene
113
-
<li>Improve the spectrum technology
112
+
<li>Mengerjakan PR
113
+
<li>Pergi belanja
114
+
<li>Minum vitamin
114
115
</ul>
115
116
);
116
117
}
@@ -122,28 +123,28 @@ img { height: 90px }
122
123
123
124
</Sandpack>
124
125
125
-
This is because JSX is stricter and has a few more rules than HTML! If you read the error messages above, they'll guide you to fix the markup, or you can follow the guide below.
126
+
Ini dikarenakan JSX lebih ketat dan memiliki banyak peraturan dibandingkan HTML! Jika anda membaca pesan *error* yang tertera, pesan tersebut akan mengarahkanmu untuk memperbaiki *markup*, atau anda bisa mengikuti panduan berikut.
126
127
127
128
<Note>
128
129
129
-
Most of the time, React's on-screen error messages will help you find where the problem is. Give them a read if you get stuck!
130
+
Umumnya, pesan *error* pada React akan memandu anda mencari sumber masalah yang ada di kode. Jangan lupa membaca pesan *error* jika anda *stuck*!
130
131
131
132
</Note>
132
133
133
-
## The Rules of JSX {/*the-rules-of-jsx*/}
134
+
## Aturan JSX {/*the-rules-of-jsx*/}
134
135
135
-
### 1. Return a single root element {/*1-return-a-single-root-element*/}
136
+
### 1. Hanya mengembalikan satu elemen {/*1-return-a-single-root-element*/}
136
137
137
-
To return multiple elements from a component, **wrap them with a single parent tag.**
138
+
Untuk mengembalikan lebih dari satu elemen, **bungkus mereka dengan satu tag *parent*.**
138
139
139
-
For example, you can use a `<div>`:
140
+
Contohnya, anda dapat menggunakan tag`<div>`:
140
141
141
142
```js {1,11}
142
143
<div>
143
-
<h1>Hedy Lamarr's Todos</h1>
144
+
<h1>Daftar Tugas Putri</h1>
144
145
<img
145
146
src="https://i.imgur.com/yXOvdOSs.jpg"
146
-
alt="Hedy Lamarr"
147
+
alt="Putri"
147
148
class="photo"
148
149
>
149
150
<ul>
@@ -153,14 +154,14 @@ For example, you can use a `<div>`:
153
154
```
154
155
155
156
156
-
If you don't want to add an extra `<div>`to your markup, you can write `<>`and`</>`instead:
157
+
Jika anda tidak ingin menambahkan `<div>`pada *markup*, anda dapat `<>`dan`</>`saja:
157
158
158
159
```js {1,11}
159
160
<>
160
-
<h1>Hedy Lamarr's Todos</h1>
161
+
<h1>Daftar Tugas Putri</h1>
161
162
<img
162
163
src="https://i.imgur.com/yXOvdOSs.jpg"
163
-
alt="Hedy Lamarr"
164
+
alt="Putri"
164
165
class="photo"
165
166
>
166
167
<ul>
@@ -169,81 +170,82 @@ If you don't want to add an extra `<div>` to your markup, you can write `<>` and
169
170
</>
170
171
```
171
172
172
-
This empty tag is called a *[Fragment.](/reference/react/Fragment)* Fragments let you group things without leaving any trace in the browser HTML tree.
173
+
*Tag* kosong di atas disebut *[Fragment.](/reference/react/Fragment)**Fragments* dapat menggabungkan beberapa *tag* tanpa memasukkan *tag* tersebut ke bagian dari HTML.
173
174
174
175
<DeepDive>
175
176
176
-
#### Why do multiple JSX tags need to be wrapped? {/*why-do-multiple-jsx-tags-need-to-be-wrapped*/}
177
+
#### Mengapa beberapa tag JSX perlu dibungkus? {/*why-do-multiple-jsx-tags-need-to-be-wrapped*/}
177
178
178
-
JSX looks like HTML, but under the hood it is transformed into plain JavaScript objects. You can't return two objects from a function without wrapping them into an array. This explains why you also can't return two JSX tags without wrapping them into another tag or a Fragment.
179
+
JSX mirip dengan HTML, namun di balik layar, mereka berubah menjadi objek *literal* JavaScript. Anda tidak bisa mengembalikan dua objek dari sebuah fungsi tanpa membungkus mereka ke sebuah senarai. Inilah mengapa anda juga tidak bisa mengembalikan dua *tag*JSX tanpa membungkus mereka menjadi sebuah *fragment*.
179
180
180
181
</DeepDive>
181
182
182
-
### 2. Close all the tags {/*2-close-all-the-tags*/}
183
+
### 2. Tutup semua tag {/*2-close-all-the-tags*/}
183
184
184
-
JSX requires tags to be explicitly closed: self-closing tags like `<img>` must become `<img />`, and wrapping tags like `<li>oranges` must be written as `<li>oranges</li>`.
185
+
Semua *tag* JSX harus dapat ditutup: *tag* tunggal seperti`<img>`harus ditulis`<img />`, dan *tag* ganda seperti`<li>oranges`harus ditulis`<li>oranges</li>`.
185
186
186
-
This is how Hedy Lamarr's image and list items look closed:
187
+
Berikut adalah gambar dan daftar tugas Putri dengan *tag* ganda:
187
188
188
189
```js {2-6,8-10}
189
190
<>
190
191
<img
191
192
src="https://i.imgur.com/yXOvdOSs.jpg"
192
-
alt="Hedy Lamarr"
193
+
alt="Putri"
193
194
class="photo"
194
195
/>
195
196
<ul>
196
-
<li>Invent new traffic lights</li>
197
-
<li>Rehearse a movie scene</li>
198
-
<li>Improve the spectrum technology</li>
197
+
<li>Mengerjakan PR</li>
198
+
<li>Pergi Belanja</li>
199
+
<li>Minum vitamin</li>
199
200
</ul>
200
201
</>
201
202
```
202
203
203
-
### 3. camelCase <s>all</s> most of the things! {/*3-camelcase-salls-most-of-the-things*/}
204
+
### 3. Ubah <s>semua</s> sebagian menjadi camelCase! {/*3-camelcase-salls-most-of-the-things*/}
204
205
205
-
JSX turns into JavaScript and attributes written in JSX become keys of JavaScript objects. In your own components, you will often want to read those attributes into variables. But JavaScript has limitations on variable names. For example, their names can't contain dashes or be reserved words like `class`.
206
+
JSX berubah menjadi JavaScript dan atribut yang dituis di JSX menjadi *key* pada objek di JavaScript. Dalam komponen, atribut akan lebih mudah dibaca sebagai *variable*. Namun JavaScript memiliki beberapa batasan dalam menamai *variable*. Contohnya, nama *variable* tidak boleh terdiri dari karakter minus dan tidak boleh menggunakan nama pesanan tertentu seperti`class`.
206
207
207
-
This is why, in React, many HTML and SVG attributes are written in camelCase. For example, instead of `stroke-width` you use `strokeWidth`. Since `class` is a reserved word, in React you write `className` instead, named after the [corresponding DOM property](https://developer.mozilla.org/en-US/docs/Web/API/Element/className):
208
+
Inilah mengapa di React, banyak atribut HTML dan SVG ditulis secara camelCase. Contohnya, `stroke-width`dapat ditulis sebagai `strokeWidth`. Dan karena `class`merupakan nama pesanan, di React kita menulisnya sebagai `className`, dinamakan sesuai dengan [versi DOM-nya](https://developer.mozilla.org/en-US/docs/Web/API/Element/className):
208
209
209
210
```js {4}
210
211
<img
211
212
src="https://i.imgur.com/yXOvdOSs.jpg"
212
-
alt="Hedy Lamarr"
213
+
alt="Putri"
213
214
className="photo"
214
215
/>
215
216
```
216
217
217
-
Youcan [findalltheseattributesinthelistofDOMcomponentprops.](/reference/react-dom/components/common) Ifyougetonewrong, don't worry—React will print a message with a possible correction to the [browser console.](https://developer.mozilla.org/docs/Tools/Browser_Console)
218
+
Anda dapat [mencari semua atribut pada list DOM component berikut.](/reference/react-dom/components/common)Jika ada yang salah, jangan takut—React akan menampilkan pesan dengan koreksi ke [konsol di peramban.](https://developer.mozilla.org/docs/Tools/Browser_Console)
218
219
219
220
<Pitfall>
220
221
221
-
For historical reasons, [`aria-*`](https://developer.mozilla.org/docs/Web/Accessibility/ARIA) and [`data-*`](https://developer.mozilla.org/docs/Learn/HTML/Howto/Use_data_attributes) attributes are written as in HTML with dashes.
222
+
Untuk beberapa alasan, atribut [`aria-*`](https://developer.mozilla.org/docs/Web/Accessibility/ARIA)dan[`data-*`](https://developer.mozilla.org/docs/Learn/HTML/Howto/Use_data_attributes)ditulis menggunakan tanda minus.
222
223
223
224
</Pitfall>
224
225
225
-
### Pro-tip: Use a JSX Converter {/*pro-tip-use-a-jsx-converter*/}
Converting all these attributes in existing markup can be tedious! We recommend using a [converter](https://transform.tools/html-to-jsx) to translate your existing HTML and SVG to JSX. Converters are very useful in practice, but it'sstillworthunderstandingwhatisgoingonsothatyoucancomfortablywriteJSXonyourown.
228
+
Mengubah atribut di markup yang sudah ada bisa menjadi membosankan! Kami sarankan untuk menggunakan *[converter](https://transform.tools/html-to-jsx)* untuk mengubah HTML dan SVG-mu menjadi JSX.
229
+
Converters are very useful in practice, but it's still worth understanding what is going on so that you can comfortably write JSX on your own.
228
230
229
-
Hereisyourfinalresult:
231
+
Berikut hasil jadinya:
230
232
231
233
<Sandpack>
232
234
233
235
```js
234
236
exportdefaultfunctionTodoList() {
235
237
return (
236
238
<>
237
-
<h1>Hedy Lamarr's Todos</h1>
239
+
<h1>Daftar Tugas Putri</h1>
238
240
<img
239
241
src="https://i.imgur.com/yXOvdOSs.jpg"
240
-
alt="Hedy Lamarr"
242
+
alt="Putri"
241
243
className="photo"
242
244
/>
243
245
<ul>
244
-
<li>Invent new traffic lights</li>
245
-
<li>Rehearse a movie scene</li>
246
-
<li>Improve the spectrum technology</li>
246
+
<li>Mengerjakan PR</li>
247
+
<li>Pergi Belanja</li>
248
+
<li>Minum vitamin</li>
247
249
</ul>
248
250
</>
249
251
);
@@ -258,34 +260,34 @@ img { height: 90px }
258
260
259
261
<Recap>
260
262
261
-
NowyouknowwhyJSXexistsandhowtouseitincomponents:
263
+
Sekarang anda paham mengapa ada JSX dan cara menggunakannya pada komponen:
0 commit comments