Skip to content

Commit f0c1b40

Browse files
dhafitfr17x
authored andcommitted
docs: translating update array section
1 parent 160521f commit f0c1b40

File tree

1 file changed

+66
-66
lines changed

1 file changed

+66
-66
lines changed

src/content/learn/updating-arrays-in-state.md

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,37 @@ Senarai (*array*) pada JavaScript dapat berubah, tetapi ketika Anda menyimpannya
1515

1616
</YouWillLearn>
1717

18-
## Updating arrays without mutation {/*updating-arrays-without-mutation*/}
18+
## Memperbarui senarai tanpa mutasi {/*updating-arrays-without-mutation*/}
1919

20-
In JavaScript, arrays are just another kind of object. [Like with objects](/learn/updating-objects-in-state), **you should treat arrays in React state as read-only.** This means that you shouldn't reassign items inside an array like `arr[0] = 'bird'`, and you also shouldn't use methods that mutate the array, such as `push()` and `pop()`.
20+
Dalam JavaScript, senarai hanyalah salah satu jenis objek. [Sama seperti objek](/learn/updating-objects-in-state), **pada React state Anda harus memperlakukan senarai sebagai *read-only*.** Ini berarti Anda tidak boleh menetapkan ulang item di dalam senarai seperti `arr[0] = 'bird'`, dan Anda juga tidak boleh menggunakan metode yang mengubah senarai, seperti `push()` dan `pop()`.
2121

22-
Instead, every time you want to update an array, you'll want to pass a *new* array to your state setting function. To do that, you can create a new array from the original array in your state by calling its non-mutating methods like `filter()` and `map()`. Then you can set your state to the resulting new array.
22+
Sebagai gantinya, setiap kali Anda ingin memperbarui sebuah senarai, Anda harus mengoper senarai *baru* ke pengaturan fungsi state Anda. Untuk melakukannya, Anda bisa membuat senarai baru dari senarai asli pada state Anda dengan memanggil metode non-mutasi seperti `filter()` dan `map()`. Kemudian Anda dapat mengatur state Anda ke senarai baru yang sudah dihasilkan.
2323

24-
Here is a reference table of common array operations. When dealing with arrays inside React state, you will need to avoid the methods in the left column, and instead prefer the methods in the right column:
24+
Berikut adalah tabel referensi operasi umum untuk senarai. Saat berurusan dengan senarai di dalam React state, Anda harus menghindari metode di kolom kiri, dan memilih metode di kolom kanan:
2525

26-
| | avoid (mutates the array) | prefer (returns a new array) |
27-
| --------- | ----------------------------------- | ------------------------------------------------------------------- |
28-
| adding | `push`, `unshift` | `concat`, `[...arr]` spread syntax ([example](#adding-to-an-array)) |
29-
| removing | `pop`, `shift`, `splice` | `filter`, `slice` ([example](#removing-from-an-array)) |
30-
| replacing | `splice`, `arr[i] = ...` assignment | `map` ([example](#replacing-items-in-an-array)) |
31-
| sorting | `reverse`, `sort` | copy the array first ([example](#making-other-changes-to-an-array)) |
26+
| | hindari (mutasi senarai) | pilih (menghasilkan senarai baru) |
27+
| -------------- | ------------------------------------- | ------------------------------------------------------------------------------- |
28+
| menambahkan | `push`, `unshift` | `concat`, `[...arr]` sintaksis penyebaran ([contoh](#adding-to-an-array)) |
29+
| menghapus | `pop`, `shift`, `splice` | `filter`, `slice` ([contoh](#removing-from-an-array)) |
30+
| mengganti | `splice`, `arr[i] = ...` *assignment* | `map` ([contoh](#replacing-items-in-an-array)) |
31+
| mengurutkan | `reverse`, `sort` | menyalin senarai terlebih dahulu ([contoh](#making-other-changes-to-an-array)) |
3232

33-
Alternatively, you can [use Immer](#write-concise-update-logic-with-immer) which lets you use methods from both columns.
33+
Atau, Anda dapat menggunakan [use Immer](#write-concise-update-logic-with-immer) yang memungkinkan Anda untuk menggunakan metode dari kedua kolom.
3434

3535
<Pitfall>
3636

37-
Unfortunately, [`slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) and [`splice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) are named similarly but are very different:
37+
Sayangnya, [`slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) dan [`splice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) diberi nama yang mirip tetapi sangat berbeda:
3838

39-
* `slice` lets you copy an array or a part of it.
40-
* `splice` **mutates** the array (to insert or delete items).
39+
* `slice` memungkinkan Anda menyalin senarai atau bagian darinya.
40+
* `splice` **memutasi** senarai (untuk menyisipkan atau menghapus item).
4141

42-
In React, you will be using `slice` (no `p`!) a lot more often because you don't want to mutate objects or arrays in state. [Updating Objects](/learn/updating-objects-in-state) explains what mutation is and why it's not recommended for state.
42+
Pada React, Anda akan lebih sering menggunakan `slice` (tanpa p!) karena Anda tidak ingin memutasi objek atau senarai pada state. [Memperbarui Objek](/learn/updating-objects-in-state) menjelaskan apa itu mutasi dan mengapa itu tidak direkomendasikan untuk state.
4343

4444
</Pitfall>
4545

46-
### Adding to an array {/*adding-to-an-array*/}
46+
### Menambahkan ke senarai {/*adding-to-an-array*/}
4747

48-
`push()` will mutate an array, which you don't want:
48+
`push()` akan memutasi senarai, yang mana tidak Anda inginkan:
4949

5050
<Sandpack>
5151

@@ -60,7 +60,7 @@ export default function List() {
6060

6161
return (
6262
<>
63-
<h1>Inspiring sculptors:</h1>
63+
<h1>Pematung yang menginspirasi:</h1>
6464
<input
6565
value={name}
6666
onChange={e => setName(e.target.value)}
@@ -70,7 +70,7 @@ export default function List() {
7070
id: nextId++,
7171
name: name,
7272
});
73-
}}>Add</button>
73+
}}>Tambah</button>
7474
<ul>
7575
{artists.map(artist => (
7676
<li key={artist.id}>{artist.name}</li>
@@ -87,18 +87,18 @@ button { margin-left: 5px; }
8787

8888
</Sandpack>
8989

90-
Instead, create a *new* array which contains the existing items *and* a new item at the end. There are multiple ways to do this, but the easiest one is to use the `...` [array spread](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#spread_in_array_literals) syntax:
90+
Sebagai gantinya, buat senarai *baru* yang berisi item yang sudah ada *dan* item baru di bagian akhir. Ada beberapa cara untuk melakukan ini, tapi yang paling mudah adalah dengan menggunakan `...` sintaksis [penyebaran senarai](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#spread_in_array_literals):
9191

9292
```js
93-
setArtists( // Replace the state
94-
[ // with a new array
95-
...artists, // that contains all the old items
96-
{ id: nextId++, name: name } // and one new item at the end
93+
setArtists( // Ganti state
94+
[ // dengan sebuah senarai baru
95+
...artists, // yang berisi item yang sudah ada
96+
{ id: nextId++, name: name } // dan item baru di bagian akhir
9797
]
9898
);
9999
```
100100

101-
Now it works correctly:
101+
Sekarang sudah berfungsi dengan benar:
102102

103103
<Sandpack>
104104

@@ -113,7 +113,7 @@ export default function List() {
113113

114114
return (
115115
<>
116-
<h1>Inspiring sculptors:</h1>
116+
<h1>Pematung yang menginspirasi:</h1>
117117
<input
118118
value={name}
119119
onChange={e => setName(e.target.value)}
@@ -123,7 +123,7 @@ export default function List() {
123123
...artists,
124124
{ id: nextId++, name: name }
125125
]);
126-
}}>Add</button>
126+
}}>Tambah</button>
127127
<ul>
128128
{artists.map(artist => (
129129
<li key={artist.id}>{artist.name}</li>
@@ -140,20 +140,20 @@ button { margin-left: 5px; }
140140

141141
</Sandpack>
142142

143-
The array spread syntax also lets you prepend an item by placing it *before* the original `...artists`:
143+
Sintaksis penyebaran senarai juga memungkinkan Anda menambahkan item dengan menempatkannya *sebelum* item asli `...artists`:
144144

145145
```js
146146
setArtists([
147147
{ id: nextId++, name: name },
148-
...artists // Put old items at the end
148+
...artists // Letakkan item lama di akhir
149149
]);
150150
```
151151

152-
In this way, spread can do the job of both `push()` by adding to the end of an array and `unshift()` by adding to the beginning of an array. Try it in the sandbox above!
152+
Dengan cara ini, penyebaran dapat melakukan `push()` dengan menambahkan ke akhir senarai dan `unshift()` dengan menambahkan ke awal senarai. Cobalah pada *sandbox* di atas!
153153

154-
### Removing from an array {/*removing-from-an-array*/}
154+
### Menghapus dari senarai {/*removing-from-an-array*/}
155155

156-
The easiest way to remove an item from an array is to *filter it out*. In other words, you will produce a new array that will not contain that item. To do this, use the `filter` method, for example:
156+
Cara termudah untuk menghapus item dari senarai adalah dengan *memfilternya*. Dengan kata lain, Anda akan menghasilkan senarai baru yang tidak berisi item tersebut. Untuk melakukannya, gunakan metode `filter`, misalnya:
157157

158158
<Sandpack>
159159

@@ -173,7 +173,7 @@ export default function List() {
173173

174174
return (
175175
<>
176-
<h1>Inspiring sculptors:</h1>
176+
<h1>Pematung yang menginspirasi:</h1>
177177
<ul>
178178
{artists.map(artist => (
179179
<li key={artist.id}>
@@ -185,7 +185,7 @@ export default function List() {
185185
)
186186
);
187187
}}>
188-
Delete
188+
Hapus
189189
</button>
190190
</li>
191191
))}
@@ -197,21 +197,21 @@ export default function List() {
197197

198198
</Sandpack>
199199

200-
Click the "Delete" button a few times, and look at its click handler.
200+
Klik tombol "Hapus" beberapa kali, dan lihat penanganan kliknya.
201201

202202
```js
203203
setArtists(
204204
artists.filter(a => a.id !== artist.id)
205205
);
206206
```
207207

208-
Here, `artists.filter(a => a.id !== artist.id)` means "create an array that consists of those `artists` whose IDs are different from `artist.id`". In other words, each artist's "Delete" button will filter _that_ artist out of the array, and then request a re-render with the resulting array. Note that `filter` does not modify the original array.
208+
Di sini, `artists.filter(a => a.id !== artist.id)` berarti "buat sebuah senarai yang berisi para artis yang memiliki *ID* berbeda dari `artist.id`". Dengan kata lain, tombol "Hapus" pada setiap artis akan memfilter *artis tersebut* dari senarai, lalu meminta render ulang dengan senarai yang dihasilkan. Ingat bahwa `filter` tidak mengubah senarai asli.
209209

210-
### Transforming an array {/*transforming-an-array*/}
210+
### Mengubah sebuah senarai {/*transforming-an-array*/}
211211

212-
If you want to change some or all items of the array, you can use `map()` to create a **new** array. The function you will pass to `map` can decide what to do with each item, based on its data or its index (or both).
212+
Jika Anda ingin mengubah beberapa atau semua item dari senarai, Anda dapat menggunakan `map()` untuk membuat senarai **baru**. Fungsi yang Anda berikan ke `map` dapat memutuskan apa yang harus dilakukan dengan setiap item, berdasarkan datanya atau indeksnya (atau keduanya).
213213

214-
In this example, an array holds coordinates of two circles and a square. When you press the button, it moves only the circles down by 50 pixels. It does this by producing a new array of data using `map()`:
214+
Dalam contoh ini, sebuah senarai menyimpan koordinat dua lingkaran dan sebuah persegi. Saat Anda menekan tombol, maka hanya akan menggeser lingkaran ke bawah sebanyak 50 piksel. Ini dilakukan dengan menghasilkan senarai data baru menggunakan `map()`:
215215

216216
<Sandpack>
217217

@@ -232,24 +232,24 @@ export default function ShapeEditor() {
232232
function handleClick() {
233233
const nextShapes = shapes.map(shape => {
234234
if (shape.type === 'square') {
235-
// No change
235+
// Tidak ada perubahan
236236
return shape;
237237
} else {
238-
// Return a new circle 50px below
238+
// Kembalikan koordinat lingkaran baru 50px ke bawah
239239
return {
240240
...shape,
241241
y: shape.y + 50,
242242
};
243243
}
244244
});
245-
// Re-render with the new array
245+
// Render ulang menggunakan senarai baru
246246
setShapes(nextShapes);
247247
}
248248

249249
return (
250250
<>
251251
<button onClick={handleClick}>
252-
Move circles down!
252+
Geser lingkarang ke bawah!
253253
</button>
254254
{shapes.map(shape => (
255255
<div
@@ -277,11 +277,11 @@ body { height: 300px; }
277277

278278
</Sandpack>
279279

280-
### Replacing items in an array {/*replacing-items-in-an-array*/}
280+
### Mengganti item dalam senarai {/*replacing-items-in-an-array*/}
281281

282-
It is particularly common to want to replace one or more items in an array. Assignments like `arr[0] = 'bird'` are mutating the original array, so instead you'll want to use `map` for this as well.
282+
Sangat umum untuk ingin mengganti satu atau lebih item dalam senarai. *Assignments* seperti `arr[0] = 'bird'` memutasi senarai asli, jadi sebagai gantinya gunakanlah `map`.
283283

284-
To replace an item, create a new array with `map`. Inside your `map` call, you will receive the item index as the second argument. Use it to decide whether to return the original item (the first argument) or something else:
284+
Untuk mengganti item, buat senarai baru dengan `map`. Di dalam fungsi `map`, Anda akan menerima indeks item sebagai argumen kedua. Gunakan untuk memutuskan apakah akan mengembalikan item asli (argumen pertama) atau yang lainnya:
285285

286286
<Sandpack>
287287

@@ -300,10 +300,10 @@ export default function CounterList() {
300300
function handleIncrementClick(index) {
301301
const nextCounters = counters.map((c, i) => {
302302
if (i === index) {
303-
// Increment the clicked counter
303+
// Penambahan saat diklik
304304
return c + 1;
305305
} else {
306-
// The rest haven't changed
306+
// Sisanya tidak berubah
307307
return c;
308308
}
309309
});
@@ -331,11 +331,11 @@ button { margin: 5px; }
331331

332332
</Sandpack>
333333

334-
### Inserting into an array {/*inserting-into-an-array*/}
334+
### Menyisipkan ke dalam senarai {/*inserting-into-an-array*/}
335335

336-
Sometimes, you may want to insert an item at a particular position that's neither at the beginning nor at the end. To do this, you can use the `...` array spread syntax together with the `slice()` method. The `slice()` method lets you cut a "slice" of the array. To insert an item, you will create an array that spreads the slice _before_ the insertion point, then the new item, and then the rest of the original array.
336+
Terkadang, Anda mungkin ingin menyisipkan item pada posisi tertentu yang bukan di awal maupun di akhir. Untuk melakukan ini, Anda dapat menggunakan sintaksis penyebaran senarai `...` bersama dengan metode `slice()`. Metode `slice()` memungkinkan Anda untuk memotong "bagian" dari senarai. Untuk menyisipkan item, Anda akan membuat senarai yang menyebarkan "bagian" *sebelum* titik penyisipan, lalu item baru, lalu selebihnya dari senarai asli.
337337

338-
In this example, the Insert button always inserts at the index `1`:
338+
Dalam contoh ini, tombol sisipkan selalu menyisipkan pada indeks `1`:
339339

340340
<Sandpack>
341341

@@ -356,13 +356,13 @@ export default function List() {
356356
);
357357

358358
function handleClick() {
359-
const insertAt = 1; // Could be any index
359+
const insertAt = 1; // Bisa dari indeks berapa saja
360360
const nextArtists = [
361-
// Items before the insertion point:
361+
// Item sebelum titik penyisipan:
362362
...artists.slice(0, insertAt),
363-
// New item:
363+
// Item baru:
364364
{ id: nextId++, name: name },
365-
// Items after the insertion point:
365+
// Item setelah titik penyisipan:
366366
...artists.slice(insertAt)
367367
];
368368
setArtists(nextArtists);
@@ -371,13 +371,13 @@ export default function List() {
371371

372372
return (
373373
<>
374-
<h1>Inspiring sculptors:</h1>
374+
<h1>Pematung yang menginspirasi:</h1>
375375
<input
376376
value={name}
377377
onChange={e => setName(e.target.value)}
378378
/>
379379
<button onClick={handleClick}>
380-
Insert
380+
Sisipkan
381381
</button>
382382
<ul>
383383
{artists.map(artist => (
@@ -395,13 +395,13 @@ button { margin-left: 5px; }
395395

396396
</Sandpack>
397397

398-
### Making other changes to an array {/*making-other-changes-to-an-array*/}
398+
### Membuat perubahan lain ke senarai {/*making-other-changes-to-an-array*/}
399399

400-
There are some things you can't do with the spread syntax and non-mutating methods like `map()` and `filter()` alone. For example, you may want to reverse or sort an array. The JavaScript `reverse()` and `sort()` methods are mutating the original array, so you can't use them directly.
400+
Ada beberapa hal yang tidak dapat Anda lakukan dengan sintaksis penyebaran dan metode non-mutasi seperti `map()` dan `filter()` saja. Misalnya, Anda mungkin ingin membalikkan atau mengurutkan senarai. Metode JavaScript `reverse()` dan `sort()` memutasikan senarai asli, sehingga Anda tidak dapat menggunakannya secara langsung.
401401

402-
**However, you can copy the array first, and then make changes to it.**
402+
**Namun, Anda dapat menyalin senarai terlebih dahulu, lalu mengubahnya.**
403403

404-
For example:
404+
Sebagai contoh:
405405

406406
<Sandpack>
407407

@@ -427,7 +427,7 @@ export default function List() {
427427
return (
428428
<>
429429
<button onClick={handleClick}>
430-
Reverse
430+
Balik
431431
</button>
432432
<ul>
433433
{list.map(artwork => (
@@ -441,17 +441,17 @@ export default function List() {
441441

442442
</Sandpack>
443443

444-
Here, you use the `[...list]` spread syntax to create a copy of the original array first. Now that you have a copy, you can use mutating methods like `nextList.reverse()` or `nextList.sort()`, or even assign individual items with `nextList[0] = "something"`.
444+
Di sini, Anda menggunakan sintaksis penyebaran `[...list]` untuk membuat salinan senarai asli terlebih dahulu. Sekarang setelah Anda memiliki salinannya, Anda dapat menggunakan metode mutasi seperti `nextList.reverse()` atau `nextList.sort()`, atau bahkan menetapkan item individual dengan `nextList[0] = "something"`.
445445

446-
However, **even if you copy an array, you can't mutate existing items _inside_ of it directly.** This is because copying is shallow--the new array will contain the same items as the original one. So if you modify an object inside the copied array, you are mutating the existing state. For example, code like this is a problem.
446+
Namun, **meskipun Anda menyalin sebuah senarai, Anda tidak dapat mengubah item yang ada *di dalamnya* secara langsung,** Ini karena penyalinan dangkal—senarai baru akan berisi item yang sama dengan yang asli. Jadi jika Anda memodifikasi objek di dalam senarai yang disalin, Anda memutasi state yang ada. Misalnya, kode seperti ini adalah masalah.
447447

448448
```js
449449
const nextList = [...list];
450-
nextList[0].seen = true; // Problem: mutates list[0]
450+
nextList[0].seen = true; // Masalah: memutasi list[0]
451451
setList(nextList);
452452
```
453453

454-
Although `nextList` and `list` are two different arrays, **`nextList[0]` and `list[0]` point to the same object.** So by changing `nextList[0].seen`, you are also changing `list[0].seen`. This is a state mutation, which you should avoid! You can solve this issue in a similar way to [updating nested JavaScript objects](/learn/updating-objects-in-state#updating-a-nested-object)--by copying individual items you want to change instead of mutating them. Here's how.
454+
Meskipun `nextList` dan `list` adalah dua senarai yang berbeda, **`nextList[0]` dan `list[0]` menunjuk ke objek yang sama.** Jadi dengan mengubah `nextList[0].seen`, Anda juga mengubah `list[0].seen`. Ini adalah mutasi state, yang harus Anda hindari! Anda dapat mengatasi masalah ini dengan cara yang mirip dengan [memperbarui objek bersarang JavaScript](/learn/updating-objects-in-state#updating-a-nested-object)—dengan menyalin setiap item yang ingin Anda ubah alih-alih memutasinya. Begini caranya.
455455

456456
## Updating objects inside arrays {/*updating-objects-inside-arrays*/}
457457

0 commit comments

Comments
 (0)