Skip to content

Commit eb4ec43

Browse files
dhafitfr17x
authored andcommitted
docs: translating update objects section
1 parent f0c1b40 commit eb4ec43

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

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

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,13 @@ setList(nextList);
453453

454454
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

456-
## Updating objects inside arrays {/*updating-objects-inside-arrays*/}
456+
## Memperbarui objek di dalam senarai {/*updating-objects-inside-arrays*/}
457457

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!
459459

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.
461461

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:
463463

464464
<Sandpack>
465465

@@ -539,34 +539,34 @@ function ItemList({ artworks, onToggle }) {
539539

540540
</Sandpack>
541541

542-
The problem is in code like this:
542+
Masalahnya ada di kode seperti ini:
543543

544544
```js
545545
const myNextList = [...myList];
546546
const artwork = myNextList.find(a => a.id === artworkId);
547-
artwork.seen = nextSeen; // Problem: mutates an existing item
547+
artwork.seen = nextSeen; // Masalah: memutasikan item yang sudah ada
548548
setMyList(myNextList);
549549
```
550550

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)*.
552552

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.**
554554

555555
```js
556556
setMyList(myList.map(artwork => {
557557
if (artwork.id === artworkId) {
558-
// Create a *new* object with changes
558+
// Buat objek baru dengan perubahan
559559
return { ...artwork, seen: nextSeen };
560560
} else {
561-
// No changes
561+
// Tidak ada perubahan
562562
return artwork;
563563
}
564564
}));
565565
```
566566

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).
568568

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:
570570

571571
<Sandpack>
572572

@@ -589,10 +589,10 @@ export default function BucketList() {
589589
function handleToggleMyList(artworkId, nextSeen) {
590590
setMyList(myList.map(artwork => {
591591
if (artwork.id === artworkId) {
592-
// Create a *new* object with changes
592+
// Buat objek baru dengan perubahan
593593
return { ...artwork, seen: nextSeen };
594594
} else {
595-
// No changes
595+
// Tidak ada perubahan
596596
return artwork;
597597
}
598598
}));
@@ -601,10 +601,10 @@ export default function BucketList() {
601601
function handleToggleYourList(artworkId, nextSeen) {
602602
setYourList(yourList.map(artwork => {
603603
if (artwork.id === artworkId) {
604-
// Create a *new* object with changes
604+
// Buat objek baru dengan perubahan
605605
return { ...artwork, seen: nextSeen };
606606
} else {
607-
// No changes
607+
// Tidak ada perubahan
608608
return artwork;
609609
}
610610
}));
@@ -652,16 +652,16 @@ function ItemList({ artworks, onToggle }) {
652652

653653
</Sandpack>
654654

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.
656656

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*/}
658658

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):
660660

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.
663663

664-
Here is the Art Bucket List example rewritten with Immer:
664+
Berikut adalah contoh Art Bucket List yang ditulis ulang dengan Immer:
665665

666666
<Sandpack>
667667

@@ -762,7 +762,7 @@ function ItemList({ artworks, onToggle }) {
762762

763763
</Sandpack>
764764

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:**
766766

767767
```js
768768
updateMyTodos(draft => {
@@ -771,9 +771,9 @@ updateMyTodos(draft => {
771771
});
772772
```
773773

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`.
775775

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.
777777

778778
<Recap>
779779

0 commit comments

Comments
 (0)