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/queueing-a-series-of-state-updates.md
+12-10Lines changed: 12 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -67,11 +67,13 @@ Ini memungkinkan Anda memperbarui beberapa variabel *state*--bahkan dari beberap
67
67
68
68
**React tidak melakukan pengelompokkan pada beberapa *event* yang disengaja, seperti klik**--setiap klik ditangani secara terpisah. Pastikan bahwa React hanya melakukan pengelompokan ketika aman untuk dilakukan. Ini memastikan bahwa, misalnya, jika klik tombol pertama menonaktifkan *form*, klik kedua tidak akan mengirimkannya lagi.
69
69
70
-
## Updating the same state multiple times before the next render {/*updating-the-same-state-multiple-times-before-the-next-render*/}
70
+
## Memperbarui state yang sama beberapa kali sebelum render selanjutnya {/*updating-the-same-state-multiple-times-before-the-next-render*/}
71
71
72
72
It is an uncommon use case, but if you would like to update the same state variable multiple times before the next render, instead of passing the *next state value* like `setNumber(number + 1)`, you can pass a *function* that calculates the next state based on the previous one in the queue, like `setNumber(n=>n+1)`. It is a way to tell React to "do something with the state value" instead of just replacing it.
73
73
74
-
Try incrementing the counter now:
74
+
Ini bukanlah penggunaan yang umum, tetapi jika Anda ingin memperbarui variabel *state* yang sama berulang kali sebelum *render* selanjutnya, alih-alih mengoper nilai *state* selanjutnya seperti `setNumber(number + 1)`, Anda dapat mengoper *function* yang menghitung *state* selanjutnya berdasarkan nilai sebelumnya pada antrean, seperti `setNumber(n => n + 1)`. Ini adalah cara untuk memberi tahu React untuk "melakukan sesuatu dengan nilai *state*" daripada hanya menggantinya.
Here, `n => n + 1`is called an **updater function.** When you pass it to a state setter:
106
+
Disini, `n => n + 1`disebut fungsi *updater.* Ketika Anda mengirimkannya ke pengatur (*setter*) state:
105
107
106
-
1. React queues this function to be processed after all the other code in the event handler has run.
107
-
2.During the next render, React goes through the queue and gives you the final updated state.
108
+
1. React mengantre fungsi ini untuk diproses setelah semua kode lain dalam *event handler* dijalankan.
109
+
2.Saat render berikutnya, React akan melewati antrean dan memberi Anda *state* terakhir yang diperbarui.
108
110
109
111
```js
110
112
setNumber(n=> n +1);
111
113
setNumber(n=> n +1);
112
114
setNumber(n=> n +1);
113
115
```
114
116
115
-
Here's how React works through these lines of code while executing the event handler:
117
+
Berikut adalah cara kerja React melalui baris kode ini saat menjalankan *event handler*:
116
118
117
119
1.`setNumber(n => n + 1)`: `n => n + 1` is a function. React adds it to a queue.
118
120
1.`setNumber(n => n + 1)`: `n => n + 1` is a function. React adds it to a queue.
119
121
1.`setNumber(n => n + 1)`: `n => n + 1` is a function. React adds it to a queue.
120
122
121
-
When you call`useState`during the next render, React goes through the queue. The previous `number`state was`0`, so that's what React passes to the first updater function as the`n` argument. Then React takes the return value of your previous updater function and passes it to the next updater as `n`, and so on:
123
+
Ketika Anda memanggil`useState`saat *render* berikutnya, React akan melewati antrean. *State*`number`sebelumnya adalah`0`, jadi itulah yang akan diteruskan React ke fungsi *updater* pertama sebagai argumen`n`. Kemudian React mengambil hasil dari fungsi *updater* sebelumnya dan meneruskannya ke *updater* berikutnya sebagai `n`, dan begitu seterusnya:
122
124
123
-
|queued update|`n`|returns|
125
+
|antrean diperbarui|`n`|hasil|
124
126
|--------------|---------|-----|
125
127
|`n => n + 1`|`0`|`0 + 1 = 1`|
126
128
|`n => n + 1`|`1`|`1 + 1 = 2`|
127
129
|`n => n + 1`|`2`|`2 + 1 = 3`|
128
130
129
-
React stores`3`as the final result and returns it from`useState`.
131
+
React menyimpan`3`sebagai hasil akhir dan mengembalikannya dari`useState`.
130
132
131
-
This is why clicking "+3" in the above example correctly increments the value by 3.
133
+
Inila mengapa mengklik "+3" pada contoh di atas dengan benar meningkatkan nilai sebesar 3.
132
134
### What happens if you update state after replacing it {/*what-happens-if-you-update-state-after-replacing-it*/}
133
135
134
136
What about this event handler? What do you think `number` will be in the next render?
0 commit comments