Skip to content

Commit c74cb44

Browse files
committed
docs: updating the same state multiple times before the next render section
1 parent 6191303 commit c74cb44

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/content/learn/queueing-a-series-of-state-updates.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ Ini memungkinkan Anda memperbarui beberapa variabel *state*--bahkan dari beberap
6767

6868
**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.
6969

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

7272
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.
7373

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.
75+
76+
Cobalah untuk menambahkan hitungan sekarang:
7577

7678
<Sandpack>
7779

@@ -101,34 +103,34 @@ h1 { display: inline-block; margin: 10px; width: 30px; text-align: center; }
101103

102104
</Sandpack>
103105

104-
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:
105107

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

109111
```js
110112
setNumber(n => n + 1);
111113
setNumber(n => n + 1);
112114
setNumber(n => n + 1);
113115
```
114116

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

117119
1. `setNumber(n => n + 1)`: `n => n + 1` is a function. React adds it to a queue.
118120
1. `setNumber(n => n + 1)`: `n => n + 1` is a function. React adds it to a queue.
119121
1. `setNumber(n => n + 1)`: `n => n + 1` is a function. React adds it to a queue.
120122

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

123-
| queued update | `n` | returns |
125+
| antrean diperbarui | `n` | hasil |
124126
|--------------|---------|-----|
125127
| `n => n + 1` | `0` | `0 + 1 = 1` |
126128
| `n => n + 1` | `1` | `1 + 1 = 2` |
127129
| `n => n + 1` | `2` | `2 + 1 = 3` |
128130

129-
React stores `3` as the final result and returns it from `useState`.
131+
React menyimpan `3` sebagai hasil akhir dan mengembalikannya dari `useState`.
130132

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.
132134
### What happens if you update state after replacing it {/*what-happens-if-you-update-state-after-replacing-it*/}
133135

134136
What about this event handler? What do you think `number` will be in the next render?

0 commit comments

Comments
 (0)