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
+44-44Lines changed: 44 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +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.
7
+
`useId`هو خطاف react يستخدم لإنشاء معرفات فريدة يمكن تمريرها إلى سمات إمكانية الوصول.
8
8
9
9
```js
10
10
constid=useId()
@@ -16,11 +16,11 @@ const id = useId()
16
16
17
17
---
18
18
19
-
## Reference {/*reference*/}
19
+
## المرجع {/*reference*/}
20
20
21
21
### `useId()` {/*useid*/}
22
22
23
-
Call`useId`at the top level of your component to generate a unique ID:
23
+
استدع`useId`في المستوى الأعلى لمكونك لإنشاء معرف فريد
24
24
25
25
```js
26
26
import { useId } from'react';
@@ -30,35 +30,35 @@ function PasswordField() {
30
30
// ...
31
31
```
32
32
33
-
[See more examples below.](#usage)
33
+
[يرجى الإطلاع على المزيد من الأمثلة بالأسفل.](#usage)
34
34
35
-
#### Parameters {/*parameters*/}
35
+
#### المعاملات (parameters) {/*parameters*/}
36
36
37
-
`useId`does not take any parameters.
37
+
`useId`لا يقبل أي معاملات.
38
38
39
-
#### Returns {/*returns*/}
39
+
#### العائدات {/*returns*/}
40
40
41
-
`useId`returns a unique ID string associated with this particular `useId`call in this particular component.
41
+
`useId`يعيد نص فريد مرتبط باستدعاء `useId`المستخدم في هذا المكون تحديدا.
42
42
43
-
#### Caveats {/*caveats*/}
43
+
#### تنبيهات {/*caveats*/}
44
44
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.
45
+
* `useId`هو خطاف، لذلك يمكنك استدعائه فقط **في المستوي الأعلي من مكونك** أو من خلال الخطافات الخاصة بك. لا يمكنك استدعاء الخطاف داخل الحلقات والشروط. إذا كنت بحاجة إلي ذلك، قم بإستخراج مكون جديد وقم بنقل الحالة إليه.
46
46
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)
47
+
* `useId` **لا ينبغي استخدامه لتوليد المفاتيح** في القائمة. [يجب أن تتم إنشاء المفاتيح من البيانات الخاصة بك.](/learn/rendering-lists#where-to-get-your-key)
48
48
49
49
---
50
50
51
-
## Usage {/*usage*/}
51
+
## الاستخدام {/*usage*/}
52
52
53
53
<Pitfall>
54
54
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)
55
+
**لا تستدعِ `useId`لتوليد المفاتيح في القائمة.** [يجب أن تتم إنشاء المفاتيح من البيانات الخاصة بك.](/learn/rendering-lists#where-to-get-your-key)
56
56
57
57
</Pitfall>
58
58
59
-
### Generating unique IDs for accessibility attributes {/*generating-unique-ids-for-accessibility-attributes*/}
59
+
### إنشاء معرفات فريدة لسمات إمكانية الوصول {/*generating-unique-ids-for-accessibility-attributes*/}
60
60
61
-
Call`useId`at the top level of your component to generate a unique ID:
61
+
استدعِ`useId`في المستوي الأعلي من المكون الخاص بك لإنشاء معرف فريد:
62
62
63
63
```js [[1, 4, "passwordHintId"]]
64
64
import { useId } from'react';
@@ -68,7 +68,7 @@ function PasswordField() {
68
68
// ...
69
69
```
70
70
71
-
You can then pass the <CodeStep step={1}>generated ID</CodeStep> to different attributes:
71
+
يمكنك بعد ذلك تمرير <CodeStep step={1}>المعرف الذى تم إنشاؤه</CodeStep> إلى سمات مختلفة:
@@ -77,11 +77,11 @@ You can then pass the <CodeStep step={1}>generated ID</CodeStep> to different at
77
77
</>
78
78
```
79
79
80
-
**Let's walk through an example to see when this is useful.**
80
+
**دعنا نستعرض مثالا لمعرفة متى يكون ذلك مفيدا.**
81
81
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).
82
+
[HTML accessibility attributes](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA) مثل [`aria-describedby`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-describedby) تتيح لك تحديد أن هناك علامتين مرتبطين ببعضهما البعض. على سبيل المثال، يمكنك تحديد أن العنصر (مثل صندوق الإدخال) يتم وصفه بواسطة عنصر آخر (مثل فقرة).
83
83
84
-
In regular HTML, you would write it like this:
84
+
في HTML العادي ستكتبه بهذا الشكل:
85
85
86
86
```html {5,8}
87
87
<label>
@@ -92,11 +92,11 @@ In regular HTML, you would write it like this:
92
92
/>
93
93
</label>
94
94
<p id="password-hint">
95
-
The password should contain at least18characters
95
+
يجب أن تحتوي كلمة السر على18حرفًا على الأقل
96
96
</p>
97
97
```
98
98
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`:
99
+
مع ذلك، تضمين المعرفات بهذا الشكل ليس طريقة جيدة في React. يمكن أن يتم عرض المكون أكثر من مرة على الصفحة، ولكن يجب أن تكون المعرفات فريدة! بدلا من تضمين معرف ثابت، يمكنك توليد معرف فريد باستخدام`useId`:
100
100
101
101
```js {4,11,14}
102
102
import { useId } from'react';
@@ -113,14 +113,14 @@ function PasswordField() {
113
113
/>
114
114
</label>
115
115
<p id={passwordHintId}>
116
-
The password should contain at least18characters
116
+
يجب أن تحتوي كلمة السر على18حرفًا على الأقل
117
117
</p>
118
118
</>
119
119
);
120
120
}
121
121
```
122
122
123
-
Now, even if `PasswordField`appears multiple times on the screen, the generated IDs won't clash.
123
+
الآن، حتى إذا كان `PasswordField`يظهر عدة مرات على الشاشة، لن تحدث تعارضات بين المعرفات المولدة.
124
124
125
125
<Sandpack>
126
126
@@ -139,7 +139,7 @@ function PasswordField() {
139
139
/>
140
140
</label>
141
141
<p id={passwordHintId}>
142
-
The password should contain at least18characters
142
+
يجب أن تحتوي كلمة السر على18حرفًا على الأقل
143
143
</p>
144
144
</>
145
145
);
@@ -148,9 +148,9 @@ function PasswordField() {
148
148
exportdefaultfunctionApp() {
149
149
return (
150
150
<>
151
-
<h2>Choose password</h2>
151
+
<h2>أدخل كلمة سر</h2>
152
152
<PasswordField />
153
-
<h2>Confirm password</h2>
153
+
<h2>تأكيد كلمة السر</h2>
154
154
<PasswordField />
155
155
</>
156
156
);
@@ -163,33 +163,33 @@ input { margin: 5px; }
163
163
164
164
</Sandpack>
165
165
166
-
[Watch this video](https://www.youtube.com/watch?v=0dNzNcuEuOo) to see the difference in the user experience with assistive technologies.
166
+
[شاهد هذا الفيديو](https://www.youtube.com/watch?v=0dNzNcuEuOo) لترى الفرق في تجربة المستخدم مع تقنيات المساعدة.
167
167
168
168
<Pitfall>
169
169
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.
170
+
في [تصيير الخادم](/reference/react-dom/server), **يتطلب `useId`وجود نفس شجرة المكونات علي الخادم والعميل**. إذا لم تتطابق الشجرات التي تقوم بعرضها على الخادم والعميل حرفيا، فإن المعرفات المولدة لن تتطابق.
171
171
172
172
</Pitfall>
173
173
174
174
<DeepDive>
175
175
176
-
#### Why is useId better than an incrementing counter? {/*why-is-useid-better-than-an-incrementing-counter*/}
176
+
#### لماذا يعد useId أفضل من العداد المتزايد؟ {/*why-is-useid-better-than-an-incrementing-counter*/}
177
177
178
-
You might be wondering why `useId`is better than incrementing a global variable like`nextId++`.
178
+
قد تتساءل لماذا `useId`أفضل من زيادة متغير عالمي مثل`nextId++`.
179
179
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.
180
+
الفائدة الأساسية لـ `useId`هي أن React ستضمن أنه يعمل مع [تصيير الخادم.](/reference/react-dom/server) أثناء تصيير الخادم، يتم تحويل مكوناتك إلي عناصر HTML. في وقت لاحق، على العميل، [hydration](/reference/react-dom/client/hydrateRoot) يقوم بربط معالجات الأحداث الخاصة بك بعناصر HTML التي تم توليدها. لكي يعمل تحويل العناصر على العميل بشكل صحيح، يجب أن يتطابق إخراج العميل مع HTML الذي على الخادم.
181
181
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.
182
+
من الصعب جدا ضمان ذلك باستخدام عداد متزايد لأن ترتيب تحويل المكونات على العميل قد لا يتطابق مع ترتيب إخراج HTML على الخادم. من خلال استدعاء `useId`، ستضمن أن عملية تحويل المكونات ستعمل بشكل صحيح، وسيتطابق الإخراج بين الخادم والعميل.
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
+
داخل React، يتم إنشاء `useId`من الـ "مسار الأب" للمكون الذي يستدعيه. وهذا هو السبب في أنه إذا كانت شجرة العميل وشجرة الخادم متطابقتين، سيتطابق "مسار لأب" بغض النظر عن ترتيب العرض.
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
+
### توليد معرفات لعدة عناصر ذات صلة {/*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
+
إذا كنت بحاجة إلى تعيين معرفات لعدة عناصر ذات صلة، يمكنك استدعاء `useId`لتوليد بادئة مشتركة لها:
193
193
194
194
<Sandpack>
195
195
@@ -200,10 +200,10 @@ export default function Form() {
This lets you avoid calling`useId`for every single element that needs a unique ID.
219
+
هذا يتيح لك تجنب استدعاء`useId`لكل عنصر يحتاج إلى معرف فريد.
220
220
221
221
---
222
222
223
-
### Specifying a shared prefix for all generated IDs {/*specifying-a-shared-prefix-for-all-generated-ids*/}
223
+
### تحديد بادئة مشتركة لجميع المعرفات المولدة {/*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
+
إذا كنت تقوم بعرض عدة تطبيقات react مستقلة على صفحة واحدة, قم بتمرير `identifierPrefix`كخيار إلى استدعاءات [`createRoot`](/reference/react-dom/client/createRoot#parameters) أو [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) الخاصة بك. هذا يضمن عدم حدوث تعارض بين المعرفات المولدة بواسطة التطبيقين المختلفين لأن كل معرف تم إنشاؤه باستخدام `useId`سيبدأ بالبادئة المميزة التي حددتها.
226
226
227
227
<Sandpack>
228
228
229
229
```html index.html
230
230
<!DOCTYPE html>
231
231
<html>
232
-
<head><title>My app</title></head>
232
+
<head><title>تطبيقي</title></head>
233
233
<body>
234
234
<div id="root1"></div>
235
235
<div id="root2"></div>
@@ -253,7 +253,7 @@ function PasswordField() {
253
253
/>
254
254
</label>
255
255
<p id={passwordHintId}>
256
-
The password should contain at least18characters
256
+
يجب أن تحتوي كلمة السر على18حرفًا على الأقل
257
257
</p>
258
258
</>
259
259
);
@@ -262,7 +262,7 @@ function PasswordField() {
262
262
exportdefaultfunctionApp() {
263
263
return (
264
264
<>
265
-
<h2>Choose password</h2>
265
+
<h2>أدخل كلمة السر</h2>
266
266
<PasswordField />
267
267
</>
268
268
);
@@ -274,7 +274,7 @@ import { createRoot } from 'react-dom/client';
0 commit comments