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
+25-25Lines changed: 25 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -453,13 +453,13 @@ setList(nextList);
453
453
454
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.
## Memperbarui objek di dalam senarai {/*updating-objects-inside-arrays*/}
457
457
458
-
Objects are not _really_ located "inside" arrays. They might appear to be "inside" in code, but each object in an array is a separate value, to which the array "points". This is why you need to be careful when changing nested fields like `list[0]`. Another person's artwork list may point to the same element of the array!
458
+
Objek *tidak benar-benar* terletak "di dalam" senarai. Mereka mungkin terlihat berada "di dalam" pada kode, tetapi setiap objek dalam senarai adalah nilai yang terpisah, yang "ditunjukkan" oleh senarai. Inilah mengapa Anda harus berhati-hati saat mengubah bagian bersarang seperti `list[0]`. Daftar *artwork* orang lain mungkin menunjuk ke elemen senarai yang sama!
459
459
460
-
**When updating nested state, you need to create copies from the point where you want to update, and all the way up to the top level.**Let's see how this works.
460
+
**Ketika mengubah state yang bersarang, Anda harus membuat salinan mulai dari titik di mana Anda ingin mengubah, hingga ke level teratas.**Mari kita lihat bagaimana ini bekerja.
461
461
462
-
In this example, two separate artwork lists have the same initial state. They are supposed to be isolated, but because of a mutation, their state is accidentally shared, and checking a box in one list affects the other list:
462
+
Dalam contoh ini, dua daftar *artwork* terpisah memiliki state awal yang sama. Mereka seharusnya terisolasi, tetapi karena adanya mutasi, state mereka secara tidak sengaja dibagikan, sehingga mencentang kotak di satu daftar akan memengaruhi daftar lainnya:
463
463
464
464
<Sandpack>
465
465
@@ -539,34 +539,34 @@ function ItemList({ artworks, onToggle }) {
artwork.seen= nextSeen; //Problem: mutates an existing item
547
+
artwork.seen= nextSeen; //Masalah: memutasikan item yang sudah ada
548
548
setMyList(myNextList);
549
549
```
550
550
551
-
Although the `myNextList` array itself is new, the *items themselves* are the same as in the original `myList`array. So changing`artwork.seen`changes the *original*artwork item. That artwork item is also in`yourList`, which causes the bug. Bugs like this can be difficult to think about, but thankfully they disappear if you avoid mutating state.
551
+
Meskipun senarai `myList` itu sendiri baru, *item-itemnya* sama dengan senarai `myList`yang asli. Jadi mengubah`artwork.seen`akan mengubah item *artwork asli*. Item *artwork* itu juga ada di`yourList`, yang menyebabkan bug. Bug seperti ini mungkin sulit untuk dipikirkan, tetapi untungnya bug tersebut akan hilang jika Anda menghindari perubahan pada state *(mutating state)*.
552
552
553
-
**You can use`map`to substitute an old item with its updated version without mutation.**
553
+
**Anda dapat menggunakan`map`untuk mengganti item lama dengan versi terbarunya tanpa mutasi.**
554
554
555
555
```js
556
556
setMyList(myList.map(artwork=> {
557
557
if (artwork.id=== artworkId) {
558
-
//Create a *new* object with changes
558
+
//Buat objek baru dengan perubahan
559
559
return { ...artwork, seen: nextSeen };
560
560
} else {
561
-
//No changes
561
+
//Tidak ada perubahan
562
562
return artwork;
563
563
}
564
564
}));
565
565
```
566
566
567
-
Here, `...`is the object spread syntax used to [create a copy of an object.](/learn/updating-objects-in-state#copying-objects-with-the-spread-syntax)
567
+
Di sini, `...`adalah sintaksis penyebaran objek yang digunakan untuk [membuat salinan objek](/learn/updating-objects-in-state#copying-objects-with-the-spread-syntax).
568
568
569
-
With this approach, none of the existing state items are being mutated, and the bug is fixed:
569
+
Dengan pendekatan ini, item state yang ada tidak akan dimutasi, dan bug teratasi:
570
570
571
571
<Sandpack>
572
572
@@ -589,10 +589,10 @@ export default function BucketList() {
589
589
functionhandleToggleMyList(artworkId, nextSeen) {
590
590
setMyList(myList.map(artwork=> {
591
591
if (artwork.id=== artworkId) {
592
-
//Create a *new* object with changes
592
+
//Buat objek baru dengan perubahan
593
593
return { ...artwork, seen: nextSeen };
594
594
} else {
595
-
//No changes
595
+
//Tidak ada perubahan
596
596
return artwork;
597
597
}
598
598
}));
@@ -601,10 +601,10 @@ export default function BucketList() {
@@ -652,16 +652,16 @@ function ItemList({ artworks, onToggle }) {
652
652
653
653
</Sandpack>
654
654
655
-
In general, **you should only mutate objects that you have just created.**If you were inserting a *new*artwork, you could mutate it, but if you're dealing with something that's already in state, you need to make a copy.
655
+
Secara umum, **Anda sebaiknya hanya memutasi objek yang baru saja Anda buat.**Jika Anda memasukkan *artwork baru*, Anda dapat memutasinya, tetapi jika Anda berurusan dengan state yang sudah ada, Anda perlu membuat salinannya.
656
656
657
-
### Write concise update logic with Immer {/*write-concise-update-logic-with-immer*/}
657
+
### Menulis logika pembaruan singkat dengan Immer {/*write-concise-update-logic-with-immer*/}
658
658
659
-
Updating nested arrays without mutation can get a little bit repetitive. [Just as with objects](/learn/updating-objects-in-state#write-concise-update-logic-with-immer):
659
+
Memperbarui senarai bersarang tanpa mutasi bisa jadi sedikit berulang. [Sama seperti objek:](/learn/updating-objects-in-state#write-concise-update-logic-with-immer):
660
660
661
-
-Generally, you shouldn't need to update state more than a couple of levels deep. If your state objects are very deep, you might want to [restructure them differently](/learn/choosing-the-state-structure#avoid-deeply-nested-state)so that they are flat.
662
-
-If you don't want to change your state structure, you might prefer to use [Immer](https://github.com/immerjs/use-immer), which lets you write using the convenient but mutating syntax and takes care of producing the copies for you.
661
+
-Secara umum, Anda tidak perlu memperbarui state lebih dari beberapa level kedalaman. Jika state objek Anda sangat dalam, Anda mungkin ingin [menyusunnya kembali secara berbeda](/learn/choosing-the-state-structure#avoid-deeply-nested-state)sehingga menjadi rata.
662
+
-Jika Anda tidak ingin mengubah struktur state Anda, Anda mungkin lebih memilih untuk menggunakan [Immer](https://github.com/immerjs/use-immer), yang memungkinkan Anda menulis menggunakan sintaksis yang mudah tetapi dapat mengubah state dan mengurus penyalinannya untuk Anda.
663
663
664
-
Here is the Art Bucket List example rewritten with Immer:
664
+
Berikut adalah contoh Art Bucket List yang ditulis ulang dengan Immer:
665
665
666
666
<Sandpack>
667
667
@@ -762,7 +762,7 @@ function ItemList({ artworks, onToggle }) {
762
762
763
763
</Sandpack>
764
764
765
-
Note how with Immer, **mutation like`artwork.seen = nextSeen`is now okay:**
765
+
Perhatikan bagaimana dengan Immer, **mutasi seperti`artwork.seen = nextSeen`sekarang baik-baik saja:**
766
766
767
767
```js
768
768
updateMyTodos(draft=> {
@@ -771,9 +771,9 @@ updateMyTodos(draft => {
771
771
});
772
772
```
773
773
774
-
This is because you're not mutating the _original_state, but you're mutating a special `draft`object provided by Immer. Similarly, you can apply mutating methods like`push()`and`pop()`to the content of the`draft`.
774
+
Ini karena Anda tidak mengubah state*aslinya*, tetapi Anda mengubah objek `draft`khusus yang disediakan oleh Immer. Demikian pula, Anda dapat menerapkan metode mutasi seperti`push()`dan`pop()`ke konten`draft`.
775
775
776
-
Behind the scenes, Immer always constructs the next state from scratch according to the changes that you've done to the `draft`. This keeps your event handlers very concise without ever mutating state.
776
+
Di belakang layar, Immer selalu membuat state berikutnya dari awal sesuai dengan perubahan yang Anda lakukan pada `draft`. Ini membuat *event handler* Anda sangat ringkas tanpa pernah mengubah state.
0 commit comments