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/reference/react/useId.md
+33-33Lines changed: 33 additions & 33 deletions
Original file line number
Diff line number
Diff line change
@@ -4,8 +4,7 @@ title: useId
4
4
5
5
<Intro>
6
6
7
-
`useId` is a React Hook for generating unique IDs that can be passed to accessibility attributes.
8
-
7
+
`useId` adalah React Hook untuk menghasilkan ID unik yang dapat diteruskan ke atribut aksesibilitas.
9
8
```js
10
9
constid=useId()
11
10
```
@@ -16,11 +15,11 @@ const id = useId()
16
15
17
16
---
18
17
19
-
## Reference {/*reference*/}
18
+
## Referensi {/*reference*/}
20
19
21
20
### `useId()` {/*useid*/}
22
21
23
-
Call`useId`at the top level of your component to generate a unique ID:
22
+
Panggil`useId`di tingkat teratas komponen Anda untuk menghasilkan ID unik:
24
23
25
24
```js
26
25
import { useId } from'react';
@@ -30,35 +29,35 @@ function PasswordField() {
30
29
// ...
31
30
```
32
31
33
-
[See more examples below.](#usage)
32
+
[Lihat lebih banyak contoh di bawah ini.](#usage)
34
33
35
-
#### Parameters {/*parameters*/}
34
+
#### Parameter {/*parameters*/}
36
35
37
-
`useId`does not take any parameters.
36
+
`useId`tidak mengambil parameter apapun.
38
37
39
-
#### Returns {/*returns*/}
38
+
#### Kembalian {/*returns*/}
40
39
41
-
`useId`returns a unique ID string associated with this particular`useId`call in this particular component.
40
+
`useId`mengembalikan string ID unik yang terkait dengan panggilan`useId`tertentu dalam komponen khusus ini.
42
41
43
42
#### Caveats {/*caveats*/}
44
43
45
-
* `useId`is a Hook, so you can only call it **at the top level of your component** or your own Hooks. You can't call it inside loops or conditions. If you need that, extract a new component and move the state into it.
44
+
* `useId`adalah sebuah Hook, jadi Anda hanya dapat memanggilnya **di tingkat teratas komponen Anda** atau Hook Anda sendiri. Anda tidak dapat memanggilnya di dalam perulangan (*loops*) atau kondisi (*conditions*). Jika Anda membutuhkannya, ekstrak komponen baru dan pindahkan *state* ke dalamnya.
46
45
47
-
* `useId` **should not be used to generate keys** in a list. [Keys should be generated from your data.](/learn/rendering-lists#where-to-get-your-key)
46
+
* `useId` **tidak boleh digunakan untuk menghasilkan *key*** dalam daftar. [*Key* harus dihasilkan dari data Anda.](/learn/rendering-lists#where-to-get-your-key)
48
47
49
48
---
50
49
51
-
## Usage {/*usage*/}
50
+
## Pengunaan {/*usage*/}
52
51
53
52
<Pitfall>
54
53
55
-
**Do not call `useId`to generate keys in a list.** [Keys should be generated from your data.](/learn/rendering-lists#where-to-get-your-key)
54
+
**Jangan panggil `useId`untuk menghasilkan *key* dalam daftar.** [*Key* harus dihasilkan dari data Anda](/learn/rendering-lists#where-to-get-your-key)
56
55
57
56
</Pitfall>
58
57
59
-
### Generating unique IDs for accessibility attributes {/*generating-unique-ids-for-accessibility-attributes*/}
58
+
### Menghasilkan ID unik untuk atribut aksesibilitas {/*generating-unique-ids-for-accessibility-attributes*/}
60
59
61
-
Call`useId`at the top level of your component to generate a unique ID:
60
+
Panggil`useId`di tingkat atas komponen Anda untuk menghasilkan ID unik:
62
61
63
62
```js [[1, 4, "passwordHintId"]]
64
63
import { useId } from'react';
@@ -68,7 +67,7 @@ function PasswordField() {
68
67
// ...
69
68
```
70
69
71
-
You can then pass the <CodeStep step={1}>generated ID</CodeStep> to different attributes:
70
+
Anda kemudian dapat meneruskan <CodeStep step={1}>ID yang dihasilkan</CodeStep> ke atribut yang berbeda:
@@ -77,11 +76,11 @@ You can then pass the <CodeStep step={1}>generated ID</CodeStep> to different at
77
76
</>
78
77
```
79
78
80
-
**Let's walk through an example to see when this is useful.**
79
+
**Mari telusuri contoh untuk melihat kapan ini berguna.**
81
80
82
-
[HTML accessibility attributes](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA) like [`aria-describedby`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-describedby) let you specify that two tags are related to each other. For example, you can specify that an element (like an input) is described by another element (like a paragraph).
81
+
[Atribut aksesibilitas HTML](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA) seperti [`aria-describedby`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-describedby) membiarkan Anda menentukan bahwa dua *tag* terkait satu sama lain. Misalnya, Anda dapat menentukan bahwa suatu elemen (seperti sebuah masukan (`input`)) dijelaskan oleh elemen lain (seperti sebuah paragraf (`p`)).
83
82
84
-
In regular HTML, you would write it like this:
83
+
Dalam HTML biasa, Anda akan menulisnya seperti ini:
85
84
86
85
```html {5,8}
87
86
<label>
@@ -96,7 +95,7 @@ In regular HTML, you would write it like this:
96
95
</p>
97
96
```
98
97
99
-
However, hardcoding IDs like this is not a good practice in React. A component may be rendered more than once on the page--but IDs have to be unique! Instead of hardcoding an ID, generate a unique ID with`useId`:
98
+
Namun, menuliskan ID secara langsung di dalam kode (*hardcoding*) seperti ini bukanlah praktik yang baik di React. Sebuah komponen dapat di-*render* lebih dari sekali pada halaman—namun ID harus unik! Alih-alih melakukan pemaksaan ID, buat ID unik dengan`useId`:
100
99
101
100
```js {4,11,14}
102
101
import { useId } from'react';
@@ -120,7 +119,7 @@ function PasswordField() {
120
119
}
121
120
```
122
121
123
-
Now, even if `PasswordField`appears multiple times on the screen, the generated IDs won't clash.
122
+
Sekarang, meskipun `PasswordField`muncul beberapa kali di layar, ID yang dihasilkan tidak akan berbenturan.
124
123
125
124
<Sandpack>
126
125
@@ -163,33 +162,34 @@ input { margin: 5px; }
163
162
164
163
</Sandpack>
165
164
166
-
[Watch this video](https://www.youtube.com/watch?v=0dNzNcuEuOo) to see the difference in the user experience with assistive technologies.
165
+
[Tonton video ini](https://www.youtube.com/watch?v=0dNzNcuEuOo) untuk melihat perbedaan pengalaman pengguna dengan teknologi bantu.
167
166
168
167
<Pitfall>
169
168
170
-
With [server rendering](/reference/react-dom/server), **`useId`requires an identical component tree on the server and the client**. If the trees you render on the server and the client don't match exactly, the generated IDs won't match.
169
+
Dengan [*server rendering*](/reference/react-dom/server), **`useId`membutuhkan pohon komponen yang identik di *server* dan klien**. Jika pohon yang Anda *render* di *server* dan klien tidak sama persis, ID yang dihasilkan tidak akan cocok.
171
170
172
171
</Pitfall>
173
172
174
173
<DeepDive>
175
174
176
-
#### Why is useId better than an incrementing counter? {/*why-is-useid-better-than-an-incrementing-counter*/}
175
+
#### Mengapa useId lebih baik daripada penghitung kenaikan? {/*why-is-useid-better-than-an-incrementing-counter*/}
176
+
177
+
Anda mungkin bertanya-tanya mengapa `useId` lebih baik daripada menambahkan variabel global seperti `nextId++`.
177
178
178
-
You might be wondering why `useId`is better than incrementing a global variable like `nextId++`.
179
+
Manfaat utama `useId`adalah React memastikan bahwa ia bekerja dengan [*server rendering*.](/reference/react-dom/server) Selama *server rendering*, komponen Anda menghasilkan keluaran HTML. Kemudian, pada klien, [hidrasi](/reference/react-dom/client/hydrateRoot) melampirkan *event handler* Anda ke HTML yang dihasilkan. Agar hidrasi berfungsi, output klien harus cocok dengan HTML dari *server*.
179
180
180
-
The primary benefit of `useId` is that React ensures that it works with [server rendering.](/reference/react-dom/server) During server rendering, your components generate HTML output. Later, on the client, [hydration](/reference/react-dom/client/hydrateRoot) attaches your event handlers to the generated HTML. For hydration to work, the client output must match the server HTML.
181
+
Hal ini sangat sulit untuk dijamin dengan penghitung kenaikan karena urutan di mana komponen klien terhidrasi mungkin tidak sesuai dengan urutan di mana HTML dari *server* dipancarkan. Dengan memanggil `useId`, Anda memastikan bahwa hidrasi akan berfungsi, dan hasilnya akan cocok antara *server* dan klien.
181
182
182
-
This is very difficult to guarantee with an incrementing counter because the order in which the client components are hydrated may not match the order in which the server HTML was emitted. By calling `useId`, you ensure that hydration will work, and the output will match between the server and the client.
183
183
184
-
Inside React, `useId`is generated from the "parent path" of the calling component. This is why, if the client and the server tree are the same, the "parent path" will match up regardless of rendering order.
184
+
Di dalam React, `useId`dihasilkan dari “jalur induk” dari komponen pemanggil. Inilah sebabnya, jika pohon di klien dan *server* sama, "jalur induk" akan cocok terlepas dari urutan *rendering*.
185
185
186
186
</DeepDive>
187
187
188
188
---
189
189
190
-
### Generating IDs for several related elements {/*generating-ids-for-several-related-elements*/}
190
+
### Menghasilkan ID untuk beberapa elemen terkait {/*generating-ids-for-several-related-elements*/}
191
191
192
-
If you need to give IDs to multiple related elements, you can call`useId`to generate a shared prefix for them:
192
+
Jika Anda perlu memberikan ID ke beberapa elemen terkait, Anda dapat memanggil`useId`untuk menghasilkan awalan bersama untuk mereka:
193
193
194
194
<Sandpack>
195
195
@@ -216,13 +216,13 @@ input { margin: 5px; }
216
216
217
217
</Sandpack>
218
218
219
-
This lets you avoid calling`useId`for every single element that needs a unique ID.
219
+
Ini memungkinkan Anda menghindari pemanggilan`useId`untuk setiap elemen yang membutuhkan ID unik.
220
220
221
221
---
222
222
223
-
### Specifying a shared prefix for all generated IDs {/*specifying-a-shared-prefix-for-all-generated-ids*/}
223
+
### Menentukan awalan bersama untuk semua ID yang dihasilkan {/*specifying-a-shared-prefix-for-all-generated-ids*/}
224
224
225
-
If you render multiple independent React applications on a single page, pass`identifierPrefix`as an option to your [`createRoot`](/reference/react-dom/client/createRoot#parameters) or [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) calls. This ensures that the IDs generated by the two different apps never clash because every identifier generated with `useId`will start with the distinct prefix you've specified.
225
+
Jika Anda me-*render* beberapa aplikasi React independen pada satu halaman, berikan`identifierPrefix`sebagai opsi untuk panggilan [`createRoot`](/reference/react-dom/client/createRoot#parameters) atau [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) Anda. Hal ini memastikan bahwa ID yang dihasilkan oleh dua aplikasi berbeda tidak pernah berbenturan karena setiap pengenal yang dibuat dengan `useId`akan dimulai dengan awalan berbeda yang telah Anda tentukan.
0 commit comments