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
Notice that all the sandboxes above show an error in the console:
271
+
Perhatikan bahwa setiap *sandbox* di atas menampilkan pesan kesalahan:
272
272
273
273
<ConsoleBlocklevel="error">
274
274
275
275
Warning: Each child in a list should have a unique "key" prop.
276
276
277
277
</ConsoleBlock>
278
278
279
-
You need to give each array item a`key` -- a string or a number that uniquely identifies it among other items in that array:
279
+
Anda harus memberikan setiap anggota dari seranai sebuah`key`--sebuah *string* atau angka yang dapat mengidentifikasi setiap anggota secara unik:
280
280
281
281
```js
282
282
<li key={person.id}>...</li>
283
283
```
284
284
285
285
<Note>
286
286
287
-
JSX elements directly inside a`map()`call always need keys!
287
+
Setiap elemen JSX yang ada di dalam`map()`harus selalu memiliki `key`!
288
288
289
289
</Note>
290
290
291
-
Keys tell React which array item each component corresponds to, so that it can match them up later. This becomes important if your array items can move (e.g. due to sorting), get inserted, or get deleted. A well-chosen `key`helps React infer what exactly has happened, and make the correct updates to the DOM tree.
291
+
`Key` memberi tahu React mengenai hubungan antara sebuah komponen dengan sebuah anggota seranai. Hal ini krusial jika anggota seranai bisa berubah posisi (contohnya karena proses pengurutan (*sorting*)), ditambah, atau dihapus. Sebuah `key`yang baik dapat membantu React untuk mengetahui apa yang telah terjadi dan menentukan langkah optimal saat memperbarui pohon DOM.
292
292
293
-
Rather than generating keys on the fly, you should include them in your data:
293
+
Sebaiknya, `key` terkandung di dalam data Anda:
294
294
295
295
<Sandpack>
296
296
@@ -318,31 +318,31 @@ export default function List() {
318
318
319
319
```js data.js active
320
320
exportconstpeople= [{
321
-
id:0, //Used in JSX as a key
321
+
id:0, //Digunakan sebagai key pada JSX
322
322
name:'Creola Katherine Johnson',
323
323
profession:'mathematician',
324
324
accomplishment:'spaceflight calculations',
325
325
imageId:'MK3eW3A'
326
326
}, {
327
-
id:1, //Used in JSX as a key
327
+
id:1, //Digunakan sebagai key pada JSX
328
328
name:'Mario José Molina-Pasquel Henríquez',
329
329
profession:'chemist',
330
330
accomplishment:'discovery of Arctic ozone hole',
331
331
imageId:'mynHUSa'
332
332
}, {
333
-
id:2, //Used in JSX as a key
333
+
id:2, //Digunakan sebagai key pada JSX
334
334
name:'Mohammad Abdus Salam',
335
335
profession:'physicist',
336
336
accomplishment:'electromagnetism theory',
337
337
imageId:'bE7W1ji'
338
338
}, {
339
-
id:3, //Used in JSX as a key
339
+
id:3, //Digunakan sebagai key pada JSX
340
340
name:'Percy Lavon Julian',
341
341
profession:'chemist',
342
342
accomplishment:'pioneering cortisone drugs, steroids and birth control pills',
343
343
imageId:'IOjWm71'
344
344
}, {
345
-
id:4, //Used in JSX as a key
345
+
id:4, //Digunakan sebagai key pada JSX
346
346
name:'Subrahmanyan Chandrasekhar',
347
347
profession:'astrophysicist',
348
348
accomplishment:'white dwarf star mass calculations',
#### Displaying several DOM nodes for each list item {/*displaying-several-dom-nodes-for-each-list-item*/}
379
+
#### Menampilkan beberapa *node*DOM untuk setiap anggota seranai {/*displaying-several-dom-nodes-for-each-list-item*/}
380
380
381
-
What do you do when each item needs to render not one, but several DOM nodes?
381
+
Bagaimana kalau setiap anggota seranai membutuhkan bukan hanya satu, tetapi beberapa *node* DOM?
382
382
383
-
The short[`<>...</>` Fragment](/reference/react/Fragment)syntax won't let you pass a key, so you need to either group them into a single `<div>`, or use the slightly longer and [more explicit `<Fragment>`syntax:](/reference/react/Fragment#rendering-a-list-of-fragments)
383
+
Sintaks singkat[`<>...</>` Fragment](/reference/react/Fragment)tidak memberikan tempat untuk `key`, sehingga Anda harus membungkus ke dalam sebuah `<div>` atau menggunakan [sintaks `<Fragment>`yang lebih panjang](/reference/react/Fragment#rendering-a-list-of-fragments):
Fragments disappear from the DOM, so this will produce a flat list of`<h1>`, `<p>`, `<h1>`, `<p>`, and so on.
398
+
Pada akhirnya, `Fragment` akan hilang dari DOM dan hanya menyisakan`<h1>`, `<p>`, `<h1>`, `<p>`, dan seterusnya, pada contoh di atas.
399
399
400
400
</DeepDive>
401
401
402
-
### Where to get your `key` {/*where-to-get-your-key*/}
402
+
### Bagaimana Anda memperoleh `key`? {/*where-to-get-your-key*/}
403
403
404
-
Different sources of data provide different sources of keys:
404
+
Sumber data yang berbeda akan memberikan `key` yang berbeda juga, contoh:
405
405
406
-
***Data from a database:**If your data is coming from a database, you can use the database keys/IDs, which are unique by nature.
407
-
***Locally generated data:**If your data is generated and persisted locally (e.g. notes in a note-taking app), use an incrementing counter, [`crypto.randomUUID()`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID) or a package like [`uuid`](https://www.npmjs.com/package/uuid) when creating items.
406
+
***Data dari basis data:**Jika data yang digunakan berasal dari basis data, ID dapat digunakan sebagai `key` karena sifatnya yang sudah unik.
407
+
***Data lokal:**Jika data dihasilkan dan disimpan secara lokal (seperti catatan pada aplikasi penulisan catatan), Anda dapat menggunakan *counter* yang bertambah, [`crypto.randomUUID()`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID), atau *library* seperti [`uuid`](https://www.npmjs.com/package/uuid).
408
408
409
-
### Rules of keys {/*rules-of-keys*/}
409
+
### Aturan `key` {/*rules-of-keys*/}
410
410
411
-
***Keys must be unique among siblings.**However, it’s okay to use the same keys for JSX nodes in _different_ arrays.
412
-
***Keys must not change**or that defeats their purpose! Don't generate them while rendering.
411
+
***`Key` harus bersifat unik antar-*sibling*.**Namun, `key` yang sama bisa digunakan lagi di seranai yang berbeda.
412
+
***`Key` tidak boleh berubah**atau fungsinya akan hilang! Jangan membuat `key` di saat me-*render*.
413
413
414
-
### Why does React need keys? {/*why-does-react-need-keys*/}
Imagine that files on your desktop didn't have names. Instead, you'd refer to them by their order -- the first file, the second file, and so on. You could get used to it, but once you delete a file, it would get confusing. The second file would become the first file, the third file would be the second file, and so on.
416
+
Bayangkan jika *file* yang ada di komputer Anda tidak memiliki nama. Sebagai pengganti, Anda merujuk ke *file* tersebut dengan urutannya--file pertama, file kedua, dan seterusnya. Saat *file* pertama dihapus, *file* kedua akan menjadi *file* pertama, kemudian *file* ketiga akan menjadi *file* kedua, dan seterusnya. Perujukan *file* menjadi tidak konsisten.
417
417
418
-
File names in a folder and JSX keys in an array serve a similar purpose. They let us uniquely identify an item between its siblings. A well-chosen key provides more information than the position within the array. Even if the _position_ changes due to reordering, the `key` lets React identify the item throughout its lifetime.
418
+
Nama file pada folder dan `key` pada JSX memiliki tujuan yang serupa. Mereka membantu kita mengidentifikasi anggota seranai dan membedakan mereka antar-*sibling*. `Key` yang baik tidak hanya memberikan informasi mengenai posisi *item* di dalam seranai, tetapi juga membantu React mengidentifikasi *item* tersebut sepanjang hidupnya.
419
419
420
420
<Pitfall>
421
421
422
-
You might be tempted to use an item's index in the array as its key. In fact, that's what React will use if you don't specify a `key` at all. But the order in which you render items will change over time if an item is inserted, deleted, or if the array gets reordered. Index as a key often leads to subtle and confusing bugs.
422
+
Anda mungkin tertarik untuk menggunakan indeks pada seranai sebagai `key`. Sebenarnya, itulah yang digunakan React jika Anda tidak memberikan `key`. Namun, urutan anggota seranai dapat berubah saat ada yang ditambah, dihapus, atau diubah urutannya. Jika ini terjadi, ini bisa menimbulkan *bug* yang membingungkan dan sulit ditemukan.
423
423
424
-
Similarly, do not generate keys on the fly, e.g. with `key={Math.random()}`. This will cause keys to never match up between renders, leading to all your components and DOM being recreated every time. Not only is this slow, but it will also lose any user input inside the list items. Instead, use a stable ID based on the data.
424
+
Bukan hanya itu, sebaiknya juga tidak membuat `key` sembarangan, misal `key={Math.random()}`. Hal ini menyebabkan `key` baru dibuat setiap *render*. Akibatnya, komponen dan DOM juga ikut dibuat baru setiap *render*. Pada akhirnya, proses *render* akan menjadi lambat dan masukan dari pengguna juga akan hilang. Oleh karena itu, sebaiknya Anda menggunakan ID yang konsisten yang berasal dari data.
425
425
426
-
Note that your components won't receive `key` as a prop. It's only used as a hint by React itself. If your component needs an ID, you have to pass it as a separate prop:`<Profile key={id} userId={id} />`.
426
+
Perlu diperhatikan bahwa komponen tidak bisa membaca `key`, layaknya sebuah *prop*. `Key` hanya digunakan oleh React. Jika komponen Anda membutuhkan ID, Anda harus menggunakan *prop* yang berbeda, contoh`<Profile key={id} userId={id} />`.
0 commit comments