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/updating-arrays-in-state.md
+66-66Lines changed: 66 additions & 66 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,37 +15,37 @@ Senarai (*array*) pada JavaScript dapat berubah, tetapi ketika Anda menyimpannya
15
15
16
16
</YouWillLearn>
17
17
18
-
## Updating arrays without mutation {/*updating-arrays-without-mutation*/}
18
+
## Memperbarui senarai tanpa mutasi {/*updating-arrays-without-mutation*/}
19
19
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()`.
21
21
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.
23
23
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:
25
25
26
-
|| avoid (mutates the array) | prefer (returns a new array)|
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.
34
34
35
35
<Pitfall>
36
36
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:
38
38
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).
41
41
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.
43
43
44
44
</Pitfall>
45
45
46
-
### Adding to an array {/*adding-to-an-array*/}
46
+
### Menambahkan ke senarai {/*adding-to-an-array*/}
47
47
48
-
`push()`will mutate an array, which you don't want:
48
+
`push()`akan memutasi senarai, yang mana tidak Anda inginkan:
49
49
50
50
<Sandpack>
51
51
@@ -60,7 +60,7 @@ export default function List() {
60
60
61
61
return (
62
62
<>
63
-
<h1>Inspiring sculptors:</h1>
63
+
<h1>Pematung yang menginspirasi:</h1>
64
64
<input
65
65
value={name}
66
66
onChange={e=>setName(e.target.value)}
@@ -70,7 +70,7 @@ export default function List() {
70
70
id: nextId++,
71
71
name: name,
72
72
});
73
-
}}>Add</button>
73
+
}}>Tambah</button>
74
74
<ul>
75
75
{artists.map(artist=> (
76
76
<li key={artist.id}>{artist.name}</li>
@@ -87,18 +87,18 @@ button { margin-left: 5px; }
87
87
88
88
</Sandpack>
89
89
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):
91
91
92
92
```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
97
97
]
98
98
);
99
99
```
100
100
101
-
Now it works correctly:
101
+
Sekarang sudah berfungsi dengan benar:
102
102
103
103
<Sandpack>
104
104
@@ -113,7 +113,7 @@ export default function List() {
113
113
114
114
return (
115
115
<>
116
-
<h1>Inspiring sculptors:</h1>
116
+
<h1>Pematung yang menginspirasi:</h1>
117
117
<input
118
118
value={name}
119
119
onChange={e=>setName(e.target.value)}
@@ -123,7 +123,7 @@ export default function List() {
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`:
144
144
145
145
```js
146
146
setArtists([
147
147
{ id: nextId++, name: name },
148
-
...artists //Put old items at the end
148
+
...artists //Letakkan item lama di akhir
149
149
]);
150
150
```
151
151
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!
153
153
154
-
### Removing from an array {/*removing-from-an-array*/}
154
+
### Menghapus dari senarai {/*removing-from-an-array*/}
155
155
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:
157
157
158
158
<Sandpack>
159
159
@@ -173,7 +173,7 @@ export default function List() {
173
173
174
174
return (
175
175
<>
176
-
<h1>Inspiring sculptors:</h1>
176
+
<h1>Pematung yang menginspirasi:</h1>
177
177
<ul>
178
178
{artists.map(artist=> (
179
179
<li key={artist.id}>
@@ -185,7 +185,7 @@ export default function List() {
185
185
)
186
186
);
187
187
}}>
188
-
Delete
188
+
Hapus
189
189
</button>
190
190
</li>
191
191
))}
@@ -197,21 +197,21 @@ export default function List() {
197
197
198
198
</Sandpack>
199
199
200
-
Click the "Delete" button a few times, and look at its click handler.
200
+
Klik tombol "Hapus" beberapa kali, dan lihat penanganan kliknya.
201
201
202
202
```js
203
203
setArtists(
204
204
artists.filter(a=>a.id!==artist.id)
205
205
);
206
206
```
207
207
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.
209
209
210
-
### Transforming an array {/*transforming-an-array*/}
210
+
### Mengubah sebuah senarai {/*transforming-an-array*/}
211
211
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).
213
213
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()`:
215
215
216
216
<Sandpack>
217
217
@@ -232,24 +232,24 @@ export default function ShapeEditor() {
232
232
functionhandleClick() {
233
233
constnextShapes=shapes.map(shape=> {
234
234
if (shape.type==='square') {
235
-
//No change
235
+
//Tidak ada perubahan
236
236
return shape;
237
237
} else {
238
-
//Return a new circle 50px below
238
+
//Kembalikan koordinat lingkaran baru 50px ke bawah
239
239
return {
240
240
...shape,
241
241
y:shape.y+50,
242
242
};
243
243
}
244
244
});
245
-
//Re-render with the new array
245
+
//Render ulang menggunakan senarai baru
246
246
setShapes(nextShapes);
247
247
}
248
248
249
249
return (
250
250
<>
251
251
<button onClick={handleClick}>
252
-
Move circles down!
252
+
Geser lingkarang ke bawah!
253
253
</button>
254
254
{shapes.map(shape=> (
255
255
<div
@@ -277,11 +277,11 @@ body { height: 300px; }
277
277
278
278
</Sandpack>
279
279
280
-
### Replacing items in an array {/*replacing-items-in-an-array*/}
280
+
### Mengganti item dalam senarai {/*replacing-items-in-an-array*/}
281
281
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`.
283
283
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:
285
285
286
286
<Sandpack>
287
287
@@ -300,10 +300,10 @@ export default function CounterList() {
300
300
functionhandleIncrementClick(index) {
301
301
constnextCounters=counters.map((c, i) => {
302
302
if (i === index) {
303
-
//Increment the clicked counter
303
+
//Penambahan saat diklik
304
304
return c +1;
305
305
} else {
306
-
//The rest haven't changed
306
+
//Sisanya tidak berubah
307
307
return c;
308
308
}
309
309
});
@@ -331,11 +331,11 @@ button { margin: 5px; }
331
331
332
332
</Sandpack>
333
333
334
-
### Inserting into an array {/*inserting-into-an-array*/}
334
+
### Menyisipkan ke dalam senarai {/*inserting-into-an-array*/}
335
335
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.
337
337
338
-
In this example, the Insert button always inserts at the index`1`:
338
+
Dalam contoh ini, tombol sisipkan selalu menyisipkan pada indeks`1`:
339
339
340
340
<Sandpack>
341
341
@@ -356,13 +356,13 @@ export default function List() {
356
356
);
357
357
358
358
functionhandleClick() {
359
-
constinsertAt=1; //Could be any index
359
+
constinsertAt=1; //Bisa dari indeks berapa saja
360
360
constnextArtists= [
361
-
//Items before the insertion point:
361
+
//Item sebelum titik penyisipan:
362
362
...artists.slice(0, insertAt),
363
-
//New item:
363
+
//Item baru:
364
364
{ id: nextId++, name: name },
365
-
//Items after the insertion point:
365
+
//Item setelah titik penyisipan:
366
366
...artists.slice(insertAt)
367
367
];
368
368
setArtists(nextArtists);
@@ -371,13 +371,13 @@ export default function List() {
### Making other changes to an array {/*making-other-changes-to-an-array*/}
398
+
### Membuat perubahan lain ke senarai {/*making-other-changes-to-an-array*/}
399
399
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.
401
401
402
-
**However, you can copy the array first, and then make changes to it.**
402
+
**Namun, Anda dapat menyalin senarai terlebih dahulu, lalu mengubahnya.**
403
403
404
-
For example:
404
+
Sebagai contoh:
405
405
406
406
<Sandpack>
407
407
@@ -427,7 +427,7 @@ export default function List() {
427
427
return (
428
428
<>
429
429
<button onClick={handleClick}>
430
-
Reverse
430
+
Balik
431
431
</button>
432
432
<ul>
433
433
{list.map(artwork=> (
@@ -441,17 +441,17 @@ export default function List() {
441
441
442
442
</Sandpack>
443
443
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"`.
445
445
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.
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.
0 commit comments