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/reusing-logic-with-custom-hooks.md
+19-20Lines changed: 19 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -1,30 +1,30 @@
1
1
---
2
-
title: 'Reusing Logic with Custom Hooks'
2
+
title: 'Menggunakan ulang logika dengan Custom Hooks'
3
3
---
4
4
5
5
<Intro>
6
6
7
-
React comes with several built-in Hooks like`useState`, `useContext`, and`useEffect`. Sometimes, you'll wish that there was a Hook for some more specific purpose: for example, to fetch data, to keep track of whether the user is online, or to connect to a chat room. You might not find these Hooks in React, but you can create your own Hooks for your application's needs.
7
+
React dilengkapi dengan beberapa Hook bawaan seperti`useState`, `useContext`, dan`useEffect`. Terkadang, Anda mungkin ingin ada Hook untuk tujuan yang lebih spesifik: misalnya, untuk mengambil data, melacak apakah pengguna sedang online, atau terhubung ke ruang obrolan. Anda mungkin tidak menemukan Hook ini di React, tetapi Anda dapat membuat Hook Anda sendiri untuk kebutuhan aplikasi Anda.
8
8
9
9
</Intro>
10
10
11
11
<YouWillLearn>
12
12
13
-
-What custom Hooks are, and how to write your own
14
-
-How to reuse logic between components
15
-
-How to name and structure your custom Hooks
16
-
-When and why to extract custom Hooks
13
+
-Apa itu custom Hooks, dan bagaimana menulis Hooks sendiri
14
+
-Bagaimana cara menggunakan ulang logika antara komponen
15
+
-Bagaimana memberi nama dan mengatur struktur Hooks yang dibuat sendiri
16
+
-Kapan dan mengapa harus mengekstrak custom Hooks
17
17
18
18
</YouWillLearn>
19
19
20
-
## Custom Hooks: Sharing logic between components {/*custom-hooks-sharing-logic-between-components*/}
Imagine you're developing an app that heavily relies on the network (as most apps do). You want to warn the user if their network connection has accidentally gone off while they were using your app. How would you go about it? It seems like you'll need two things in your component:
22
+
Bayangkan Anda sedang mengembangkan aplikasi yang sangat bergantung pada jaringan (seperti kebanyakan aplikasi). Anda ingin memberi peringatan kepada pengguna jika koneksi jaringannya tiba-tiba terputus saat mereka menggunakan aplikasi Anda. Bagaimana cara melakukannya? Sepertinya Anda akan memerlukan dua hal dalam komponen Anda:
23
23
24
-
1.A piece of state that tracks whether the network is online.
25
-
2.An Effect that subscribes to the global[`online`](https://developer.mozilla.org/en-US/docs/Web/API/Window/online_event)and[`offline`](https://developer.mozilla.org/en-US/docs/Web/API/Window/offline_event)events, and updates that state.
24
+
1.Sebuah keadaan (state) yang melacak apakah jaringan online.
25
+
2.Sebuah Efek yang berlangganan (subscribes) pada peristiwa (events)[`online`](https://developer.mozilla.org/en-US/docs/Web/API/Window/online_event)dan[`offline`](https://developer.mozilla.org/en-US/docs/Web/API/Window/offline_event)global, dan memperbarui state tersebut.
26
26
27
-
This will keep your component [synchronized](/learn/synchronizing-with-effects)with the network status. You might start with something like this:
27
+
Hal ini akan menjaga [sinkronisasi](/learn/synchronizing-with-effects)komponen Anda dengan status jaringan. Anda mungkin memulainya dengan sesuatu seperti ini:
28
28
29
29
<Sandpack>
30
30
@@ -54,11 +54,11 @@ export default function StatusBar() {
54
54
55
55
</Sandpack>
56
56
57
-
Try turning your network on and off, and notice how this `StatusBar`updates in response to your actions.
57
+
Coba matikan dan nyalakan jaringan Anda, dan perhatikan bagaimana `StatusBar`ini diperbarui sebagai respons terhadap tindakan Anda.
58
58
59
-
Now imagine you *also* want to use the same logic in a different component. You want to implement a Save button that will become disabled and show "Reconnecting..." instead of "Save" while the network is off.
59
+
Sekarang bayangkan Anda *juga* ingin menggunakan logika yang sama pada komponen yang berbeda. Anda ingin mengimplementasikan tombol Simpan yang akan menjadi tidak aktif dan menunjukkan "Sedang menghubungkan kembali..." alih-alih "Simpan" saat jaringan mati.
60
60
61
-
To start, you can copy and paste the`isOnline`state and the Effect into`SaveButton`:
61
+
Untuk memulai, Anda dapat menyalin dan mem-paste state`isOnline`dan Efeknya ke dalam`SaveButton`:
62
62
63
63
<Sandpack>
64
64
@@ -96,13 +96,13 @@ export default function SaveButton() {
96
96
97
97
</Sandpack>
98
98
99
-
Verify that, if you turn off the network, the button will change its appearance.
99
+
Pastikan, jika Anda mematikan jaringan, tampilan tombol tersebut akan berubah.
100
100
101
-
These two components work fine, but the duplication in logic between them is unfortunate. It seems like even though they have different *visual appearance,* you want to reuse the logic between them.
101
+
Kedua komponen ini berfungsi dengan baik, tetapi duplikasi logika di antara keduanya tidak diinginkan. Meskipun mereka memiliki *tampilan visual* yang berbeda, Anda ingin menggunakan kembali logika yang sama di antara keduanya.
102
102
103
-
### Extracting your own custom Hook from a component {/*extracting-your-own-custom-hook-from-a-component*/}
103
+
### Mengekstrak custom Hook Anda sendiri dari sebuah komponen {/*extracting-your-own-custom-hook-from-a-component*/}
104
104
105
-
Imagine for a moment that, similar to[`useState`](/reference/react/useState)and[`useEffect`](/reference/react/useEffect), there was a built-in `useOnlineStatus`Hook. Then both of these components could be simplified and you could remove the duplication between them:
105
+
Bayangkan bahwa, serupa dengan[`useState`](/reference/react/useState)dan[`useEffect`](/reference/react/useEffect), ada sebuah Hook `useOnlineStatus`bawaan. Kemudian kedua komponen ini bisa disederhanakan dan Anda dapat menghapus duplikasi di antara keduanya:
106
106
107
107
```js {2,7}
108
108
functionStatusBar() {
@@ -124,8 +124,7 @@ function SaveButton() {
124
124
);
125
125
}
126
126
```
127
-
128
-
Although there is no such built-in Hook, you can write it yourself. Declare a function called `useOnlineStatus` and move all the duplicated code into it from the components you wrote earlier:
127
+
Meskipun tidak ada Hook bawaan seperti itu, Anda dapat menulisnya sendiri. Deklarasikan fungsi bernama `useOnlineStatus` dan pindahkan semua kode duplikat ke dalamnya dari komponen yang Anda tulis sebelumnya:
0 commit comments