diff --git a/src/content/learn/updating-objects-in-state.md b/src/content/learn/updating-objects-in-state.md index 1e23c8d3d..e2f5162a2 100644 --- a/src/content/learn/updating-objects-in-state.md +++ b/src/content/learn/updating-objects-in-state.md @@ -1,57 +1,58 @@ --- -title: Updating Objects in State +title: Memperbarui Objek dalam State --- -State can hold any kind of JavaScript value, including objects. But you shouldn't change objects that you hold in the React state directly. Instead, when you want to update an object, you need to create a new one (or make a copy of an existing one), and then set the state to use that copy. +*State* bisa menampung berbagai jenis nilai JavaScript, termasuk objek. Tetapi objek-objek yang disimpan dalam React sebaiknya tidak diperbarui secara langsung. Sebaiknya, ketika sebuah objek ingin diperbarui, Anda perlu membuat objek baru atau menyalin objek yang ingin diubah, kemudian mengubah *state* untuk menggunakan objek hasil salinan tersebut. -- How to correctly update an object in React state -- How to update a nested object without mutating it -- What immutability is, and how not to break it -- How to make object copying less repetitive with Immer +- Bagaimana cara memperbarui objek di dalam React *state* +- Bagaimana cara memperbarui objek yang bersarang tanpa melakukan mutasi +- Apa itu *immutability*, dan bagaimana agar tidak merusaknya +- Bagaimana cara mempersingkat penyalinan objek dengan Immer -## What's a mutation? {/*whats-a-mutation*/} +## Apa itu mutasi? {/*whats-a-mutation*/} -You can store any kind of JavaScript value in state. +Anda bisa menyimpan segala jenis nilai JavaScript di dalam *state*. ```js const [x, setX] = useState(0); ``` -So far you've been working with numbers, strings, and booleans. These kinds of JavaScript values are "immutable", meaning unchangeable or "read-only". You can trigger a re-render to _replace_ a value: +Sejauh ini Anda sudah bisa menggunakan angka, *string*, dan *boolean*. Nilai-nilai JavaScript tersebut bersifat *"immutable"*, yang berarti tidak bisa diubah atau *"read-only"*. Anda bisa memicu *render* ulang untuk *menimpa* sebuah nilai: ```js setX(5); ``` -The `x` state changed from `0` to `5`, but the _number `0` itself_ did not change. It's not possible to make any changes to the built-in primitive values like numbers, strings, and booleans in JavaScript. +Nilai *state* `x` berubah dari `0` menjadi `5`, tetapi *angka `0` itu sendiri* tidak berubah. Melakukan perubahan terhadap nilai-nilai primitif yang bawaan seperti angka, *string*, dan *boolean* itu mustahil di JavaScript. -Now consider an object in state: + +Sekarang pikirkan sebuah objek dalam *state*: ```js const [position, setPosition] = useState({ x: 0, y: 0 }); ``` -Technically, it is possible to change the contents of _the object itself_. **This is called a mutation:** +Secara teknis, Anda bisa memperbarui isi konten *objek itu sendiri*. **Hal ini disebut sebagai mutasi:** ```js position.x = 5; ``` -However, although objects in React state are technically mutable, you should treat them **as if** they were immutable--like numbers, booleans, and strings. Instead of mutating them, you should always replace them. +Tetapi, meskipun objek di React secara teknis bisa diubah, Anda harus memperlakukan mereka **seolah-olah** objek itu *immutable*--seperti angka, *boolean*, dan *string*. Selain melakukan mutasi, Anda seharusnya menimpa mereka. -## Treat state as read-only {/*treat-state-as-read-only*/} +## Perlakukan state seperti read-only {/*treat-state-as-read-only*/} -In other words, you should **treat any JavaScript object that you put into state as read-only.** +Dengan kata lain, Anda seharusnya **memperlakukan semua objek JavaScript yang ditaruh di dalam *state* seperti *read-only*.** -This example holds an object in state to represent the current pointer position. The red dot is supposed to move when you touch or move the cursor over the preview area. But the dot stays in the initial position: +Berikut adalah contoh kode yang menampung objek di dalam *state* untuk merepresentasikan posisi kursor saat ini. Titik merah tersebut seharusnya bergerak ketika Anda menyentuh layar atau kursor digerakkan pada daerah *preview*. Tetapi titik merah tersebut diam di tempat saja pada posisi awal: @@ -94,7 +95,7 @@ body { margin: 0; padding: 0; height: 250px; } -The problem is with this bit of code. +Masalahnya terdapat pada potongan kode berikut. ```js onPointerMove={e => { @@ -103,9 +104,9 @@ onPointerMove={e => { }} ``` -This code modifies the object assigned to `position` from [the previous render.](/learn/state-as-a-snapshot#rendering-takes-a-snapshot-in-time) But without using the state setting function, React has no idea that object has changed. So React does not do anything in response. It's like trying to change the order after you've already eaten the meal. While mutating state can work in some cases, we don't recommend it. You should treat the state value you have access to in a render as read-only. +Kode tersebut melakukan modifikasi objek yang ditempatkan ke `position` [dari render sebelumnya.](/learn/state-as-a-snapshot#rendering-takes-a-snapshot-in-time) Tetapi tanpa menggunakan fungsi yang mengubah *state*, React tidak tahu bahwa objek tersebut telah berubah. Sehingga React tidak melakukan apa-apa. Hal ini sama seperti mencoba mengubah pesanan makanan setelah makanannya dihabiskan. Meskipun mengubah *state* bisa bekerja pada kasus tertentu, kami tidak menyarankannya. Anda harus memperlakukan nilai *state* yang aksesnya Anda miliki ketika render sebagai *read-only*. -To actually [trigger a re-render](/learn/state-as-a-snapshot#setting-state-triggers-renders) in this case, **create a *new* object and pass it to the state setting function:** +Untuk [memicu render ulang](/learn/state-as-a-snapshot#setting-state-triggers-renders) pada kasus ini, **buatlah objek *baru* dan berikan objek tersebut kepada fungsi yang mengubah *state*:** ```js onPointerMove={e => { @@ -116,12 +117,11 @@ onPointerMove={e => { }} ``` -With `setPosition`, you're telling React: - -* Replace `position` with this new object -* And render this component again +Dengan `setPosition`, Anda memberi tahu React: +* Timpa `position` dengan objek baru yang diberikan +* Lalu render komponen tersebut lagi -Notice how the red dot now follows your pointer when you touch or hover over the preview area: +Perhatikan bahwa sekarang titik merah sudah mengikuti kursor Anda ketika Anda menyentuh layar atau menggerakkan kursor pada daerah *preview*: @@ -168,16 +168,16 @@ body { margin: 0; padding: 0; height: 250px; } -#### Local mutation is fine {/*local-mutation-is-fine*/} +#### Mutasi Lokal itu baik-baik saja {/*local-mutation-is-fine*/} -Code like this is a problem because it modifies an *existing* object in state: +Kode seperti ini merupakan masalah karena ia memodifikasi objek *yang sudah ada* pada *state*: ```js position.x = e.clientX; position.y = e.clientY; ``` -But code like this is **absolutely fine** because you're mutating a fresh object you have *just created*: +Tetapi kode seperti ini itu **baik-baik saja** karena Anda melakukan mutasi terhadap objek yang *baru saja Anda buat*: ```js const nextPosition = {}; @@ -186,7 +186,7 @@ nextPosition.y = e.clientY; setPosition(nextPosition); ```` -In fact, it is completely equivalent to writing this: +Sebenarnya, penulisan kode di atas sama dengan kode berikut: ```js setPosition({ @@ -195,15 +195,15 @@ setPosition({ }); ``` -Mutation is only a problem when you change *existing* objects that are already in state. Mutating an object you've just created is okay because *no other code references it yet.* Changing it isn't going to accidentally impact something that depends on it. This is called a "local mutation". You can even do local mutation [while rendering.](/learn/keeping-components-pure#local-mutation-your-components-little-secret) Very convenient and completely okay! +Mutasi hanya menjadi masalah ketika Anda mengubah objek-objek *yang sudah ada* yang berada di dalam *state*. Mengubah objek yang baru saja Anda buat itu baik-baik saja karena *belum ada kode lain yang menggunakannya.* Mengubah objek tersebut tidak akan mempengaruhi sesuatu yang bergantung pada objek tersebut secara tidak sengaja. Hal ini disebut sebagai "mutasi lokal". Anda bahkan bisa melakukan mutasi lokal [ketika melakukan render.](/learn/keeping-components-pure#local-mutation-your-components-little-secret) Sangat mudah dan baik-baik saja! - + -## Copying objects with the spread syntax {/*copying-objects-with-the-spread-syntax*/} +## Menyalin objek-objek dengan sintaksis spread {/*copying-objects-with-the-spread-syntax*/} -In the previous example, the `position` object is always created fresh from the current cursor position. But often, you will want to include *existing* data as a part of the new object you're creating. For example, you may want to update *only one* field in a form, but keep the previous values for all other fields. +Pada contoh sebelumnya, objek `position` selalu dibentuk ulang dari posisi kursor saat ini. Tetapi, pada umumnya, Anda ingin menyimpan data *sebelumnya* sebagai bagian dari objek baru yang sedang dibuat. Sebagai contoh, Anda ingin mengubah *hanya satu* data bidang pada sebuah formulir tanpa mengubah data-data sebelumnya pada bidang lainnya. -These input fields don't work because the `onChange` handlers mutate the state: +Bidang isian berikut tidak bekerja karena *handler* `onChange` mengubah *state*: @@ -232,14 +232,14 @@ export default function Form() { return ( <>