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/reacting-to-input-with-state.md
+60-59Lines changed: 60 additions & 59 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,37 +1,39 @@
1
1
---
2
-
title: Reacting to Input with State
2
+
title: الاستجابة للمدخلات باستخدام الحالة
3
3
---
4
4
5
5
<Intro>
6
6
7
-
React provides a declarative way to manipulate the UI. Instead of manipulating individual pieces of the UI directly, you describe the different states that your component can be in, and switch between them in response to the user input. This is similar to how designers think about the UI.
7
+
React توفر طريقة تصريحيّة (declarative) لتعديل واجهة المستخدم (UI). بدلا من تعديل أجزاء منفردة من واجهة المستخدم مباشرة، يمكنك وصف الحالات المختلفة التي يأخذها مكوّنك، والانتقال بينهم كاستجابة لمدخلات المستخدم. هذا مشابه لتصور المصممين عن واجهة المستخدم.
8
+
8
9
9
10
</Intro>
10
11
11
12
<YouWillLearn>
12
13
13
-
*How declarative UI programming differs from imperative UI programming
14
-
*How to enumerate the different visual states your component can be in
15
-
*How to trigger the changes between the different visual states from code
16
-
14
+
*كيف تختلف البرمجة التصريحية لواجهة المستخدم (declarative UI programming) عن البرمجة الأمرية لواجهة المستخدم (imperative UI programming)
15
+
*كيفية استعراض الحالات المرئية المختلفة التي يأخذها مكوّنك
16
+
*كيفية تنشيط التغييرات بين الحالات المرئية المختلفة من خلال الكود
17
+
17
18
</YouWillLearn>
18
19
19
-
## How declarative UI compares to imperative {/*how-declarative-ui-compares-to-imperative*/}
20
+
## كيف تُقارَن واجهة المستخدم التصريحية (declarative UI) بالأمرية (imperative) {/*how-declarative-ui-compares-to-imperative*/}
20
21
21
-
When you design UI interactions, you probably think about how the UI *changes* in response to user actions. Consider a form that lets the user submit an answer:
22
+
عندما تصمم تعاملات واجهة المستخدم، عليك غالبًا التفكير في كيفية *تغيّر* واجهة المستخدم كاستجابة لاجراءات المستخدم. فكر في نموذج يسمح للمستخدم بإرسال إجابة:
22
23
23
-
*When you type something into the form, the "Submit" button **becomes enabled.**
24
-
*When you press "Submit", both the form and the button **become disabled,**and a spinner **appears.**
25
-
*If the network request succeeds, the form **gets hidden,**and the "Thank you" message **appears.**
26
-
*If the network request fails, an error message **appears,**and the form **becomes enabled**again.
24
+
*عندما تكتب شيئًا داخل النموذج، الزر "أرسل" **يصبح مفعلًا.**
25
+
*عندما تضغط على "أرسل"، كلٌ من النموذج والزر **يصبح معطلا**ومؤشر التحميل **يظهر.**
26
+
*لو نجح طلب الشبكة، النموج **يبدأ بالاختفاء،**ورسالة "شكرًا لك" **تظهر.**
27
+
*لو فشل طلب الشبكة، رسالة خطأٍ **تظهر،**والنموذج **يصبح مفعلًا**مجددا.
27
28
28
-
In**imperative programming,** the above corresponds directly to how you implement interaction. You have to write the exact instructions to manipulate the UI depending on what just happened. Here's another way to think about this: imagine riding next to someone in a car and telling them turn by turn where to go.
29
+
في**البرمجة الأمرية (imperative programming)**، يتوافق ما ذُكر أعلاه مباشرة مع طريقة تطبيق التعاملات. عليك أن تكتب التعليمات التامة لتعديل واجهة المستخدم معتمدا على ما حصل للتو. إليك طريقة أخرى لتفكر في هذا الأمر: تخيل نفسك راكبا إلى جانب أحدهم في سيارة مع إخباره في كل منعطف تلو الآخر عن وجهة الذهاب.
29
30
30
31
<Illustrationsrc="/images/docs/illustrations/i_imperative-ui-programming.png"alt="In a car driven by an anxious-looking person representing JavaScript, a passenger orders the driver to execute a sequence of complicated turn by turn navigations." />
31
32
32
-
They don't know where you want to go, they just follow your commands. (And if you get the directions wrong, you end up in the wrong place!) It's called *imperative* because you have to "command" each element, from the spinner to the button, telling the computer *how* to update the UI.
33
+
هم لا يعلمون إلى أين تريد أن تذهب، هم يتبعون أوامرك فقط. (ولو أنك أعطيتهم الاتجاهات الخاطئة، سوف ينتهي بك المطاف لوجهة خاطئة!) هذا يطلق عليه *أمري (imperative)* لأن عليك أن "تأمر" كل عنصر، بداية من مؤشر التحميل إلى الزر، مخبرًا
34
+
الكمبيوتر عن *كيفية* تحديث واجهة المستخدم (UI).
33
35
34
-
In this example of imperative UI programming, the form is built *without* React. It only uses the browser [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model):
36
+
في هذا المثال للبرمجة الأمرية لواجهة المستخدم، النموذج مبنيّ *بدون* React. إنه يستخدم [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model) المتصفح فقط:
35
37
36
38
<Sandpack>
37
39
@@ -84,10 +86,10 @@ function submitForm(answer) {
84
86
// Pretend it's hitting the network.
85
87
returnnewPromise((resolve, reject) => {
86
88
setTimeout(() => {
87
-
if (answer.toLowerCase() =='istanbul') {
89
+
if (answer.toLowerCase() =='إسطنبول') {
88
90
resolve();
89
91
} else {
90
-
reject(newError('Good guess but a wrong answer. Try again!'));
92
+
reject(newError('توقع جيد ولكن إجابة خاطئة. حاول مرة أخرى!'));
Manipulating the UI imperatively works well enough for isolated examples, but it gets exponentially more difficult to manage in more complex systems. Imagine updating a page full of different forms like this one. Adding a new UI element or a new interaction would require carefully checking all existing code to make sure you haven't introduced a bug (for example, forgetting to show or hide something).
136
+
تعديل واجهة المستخدم (UI) أمريًّا يعمل بشكل جيد كفاية للأمثلة المعزولة، ولكنه يصبح أكثر صعوبة بشكل استثنائي عندما تدير أنظمة أكثر تعقيدًا. تخيل تحديث صفحة مليئة بالنماذج المختلفة مثل هذه هنا. إضافة عنصر واجهة مستخدم جديد سيتطلب فحص كامل الكود الموجود بحرص للتأكد من أنك لم تقم بعمل خطأ(على سبيل المثال، نسيان إظهار أو إخفاء شيء ما).
135
137
136
-
React was built to solve this problem.
138
+
React صُنعت لحل هذه المشكلة.
137
139
138
-
In React, you don't directly manipulate the UI--meaning you don't enable, disable, show, or hide components directly. Instead, you **declare what you want to show,**and React figures out how to update the UI. Think of getting into a taxi and telling the driver where you want to go instead of telling them exactly where to turn. It's the driver's job to get you there, and they might even know some shortcuts you haven't considered!
140
+
في React، لن تقوم بتعديل واجهة المستخدم مباشرة -- يعني أنك لن تقوم بتفعيل، تعطيل، إظهار، أو إخفاء مكوّنات مباشرة. بدلا عن ذلك، سوف **تصف ما تريده أن يظهر،**و React سوف تدبر كيفية تحديث واجهة المستخدم. فكر في أخذ سيارة أجرة وإخبار السائق إلي أين تريد أنت تذهب بدلا من إخباره إلي أين ينعطف. وظيفة السائق هي أن يوصلك إلى هناك، وربما قد يعرف اختصارات لم تأخذها أنت بالحسبان.
139
141
140
142
<Illustrationsrc="/images/docs/illustrations/i_declarative-ui-programming.png"alt="In a car driven by React, a passenger asks to be taken to a specific place on the map. React figures out how to do that." />
141
143
142
-
## Thinking about UI declaratively {/*thinking-about-ui-declaratively*/}
144
+
## التفكير في واجهة المستخدم (UI) تصريحيًا {/*thinking-about-ui-declaratively*/}
143
145
144
-
You've seen how to implement a form imperatively above. To better understand how to think in React, you'll walk through reimplementing this UI in React below:
146
+
لقد رأيت كيفية تنفيذ نموذج أمريَّا أعلاه. لفهم أفضل لكيفية التفكير في React، سوف تمر بإعادة تنفيذ واجهة المستخدم (UI) هذه باستخدام React أدناه:
145
147
146
-
1.**Identify**your component's different visual states
147
-
2.**Determine**what triggers those state changes
148
-
3.**Represent**the state in memory using`useState`
149
-
4.**Remove**any non-essential state variables
150
-
5.**Connect**the event handlers to set the state
148
+
1.**عيّن**الحالات المرئية المختلفة لمكوّنك
149
+
2.**حدد**ما ينشط تغيرات تلك الحالات
150
+
3.**مثّل**الحالة في الذاكرة باستخدام`useState`
151
+
4.**احذف**أيّ متغيرات حالة غير ضرورية
152
+
5.**اربط**معالجات الأحداث لتعيين الحالة
151
153
152
-
### Step 1: Identify your component's different visual states {/*step-1-identify-your-components-different-visual-states*/}
154
+
### الخطوة 1: عين الحالات المرئية المختلفة لمكوّنك {/*step-1-identify-your-components-different-visual-states*/}
153
155
154
-
In computer science, you may hear about a ["state machine"](https://en.wikipedia.org/wiki/Finite-state_machine)being in one of several “states”. If you work with a designer, you may have seen mockups for different "visual states". React stands at the intersection of design and computer science, so both of these ideas are sources of inspiration.
156
+
في علوم الحاسب، ربما تسمع عن ["آلة حالة (state machine)"](https://en.wikipedia.org/wiki/Finite-state_machine)كونها واحدة من ضمن "حالات" متعددة.إذا كنت تعمل مع مصمم، لربما رأيت نماذج تجريبية لـ"الحالات المرئية" المختلفة
155
157
156
-
First, you need to visualize all the different "states" of the UI the user might see:
158
+
أولًا، أنت تحتاج لتصور جميع "الحالات" المختلفة لواجهة المستخدم (UI) التي قد يراها المستخدم:
159
+
***فارغة**: النموذج يحتوي زر "إرسال" معطل.
160
+
***كتابة**: النموذج يحتوي زر "إرسال" مفعّل.
161
+
***إرسال**: النموذج معطل تمامًا. يتم عرض مؤشر التحميل.
162
+
***نجاح**: يتم عرض رسالة "شكرًا لك" بدلًا من النموذج.
163
+
***خطأ**: مثل حالة الكتابة، ولكن مع رسالة خطأ إضافية
157
164
158
-
***Empty**: Form has a disabled "Submit" button.
159
-
***Typing**: Form has an enabled "Submit" button.
160
-
***Submitting**: Form is completely disabled. Spinner is shown.
161
-
***Success**: "Thank you" message is shown instead of a form.
162
-
***Error**: Same as Typing state, but with an extra error message.
163
-
164
-
Just like a designer, you'll want to "mock up" or create "mocks" for the different states before you add logic. For example, here is a mock for just the visual part of the form. This mock is controlled by a prop called `status` with a default value of `'empty'`:
165
+
تمامًا مثل المصمم، سترغب في "تجربة" أو إنشاء "نماذج تجريبية" للحالات المختلفة قبل أن تضيف المنطق. على سبيل المثال، ها هو نموذج تجريبي للجزء المرئي فقط من النموذج. هذا النموذج التجريبي متحكم به بواسطة خاصيّة تدعى `statue` مع قيمة افتراضية `'empty'`:
165
166
166
167
<Sandpack>
167
168
@@ -170,19 +171,19 @@ export default function Form({
170
171
status ='empty'
171
172
}) {
172
173
if (status ==='success') {
173
-
return<h1>That's right!</h1>
174
+
return<h1>هذا صحيح!</h1>
174
175
}
175
176
return (
176
177
<>
177
-
<h2>City quiz</h2>
178
+
<h2>اختبار المدينة</h2>
178
179
<p>
179
-
In which city is there a billboard that turns air into drinkable water?
180
+
في أي مدينة يوجد لوحة إعلانية تقوم بتحويل الهواء لمياه صالحة للشرب؟
180
181
</p>
181
182
<form>
182
183
<textarea />
183
184
<br />
184
185
<button>
185
-
Submit
186
+
أرسل
186
187
</button>
187
188
</form>
188
189
</>
@@ -192,23 +193,23 @@ export default function Form({
192
193
193
194
</Sandpack>
194
195
195
-
You could call that prop anything you like, the naming is not important. Try editing `status = 'empty'` to `status = 'success'` to see the success message appear. Mocking lets you quickly iterate on the UI before you wire up any logic. Here is a more fleshed out prototype of the same component, still "controlled" by the `status` prop:
196
+
يمكنك تسمية الخاصيّة أيّ شيء تريد، التسمية ليست مهمة. جرب تعديل`status = 'empty'`إلى`status = 'success'`لترى رسالة النجاح تظهر. التجربة تتيح لك التكرار السريع على واجهة المستخدم قبل ربط أي منطق. ها هو نموذج تجريبي أكثر تفصيلًا لنفس المكوّن، يظل "متحكم" بواسطة الخاصية`status`:
196
197
197
198
<Sandpack>
198
199
199
200
```js
200
201
exportdefaultfunctionForm({
201
-
// Try'submitting','error','success':
202
+
//جرب 'submitting'، 'error'، 'success':
202
203
status ='empty'
203
204
}) {
204
205
if (status ==='success') {
205
-
return <h1>That's right!</h1>
206
+
return<h1>هذا صحيح!</h1>
206
207
}
207
208
return (
208
209
<>
209
210
<h2>City quiz</h2>
210
211
<p>
211
-
In which city is there a billboard that turns air into drinkable water?
212
+
في أي مدينة يوجد لوحة إعلانية تقوم بتحويل الهواء لمياه صالحة للشرب؟
212
213
</p>
213
214
<form>
214
215
<textarea disabled={
@@ -219,11 +220,11 @@ export default function Form({
219
220
status ==='empty'||
220
221
status ==='submitting'
221
222
}>
222
-
Submit
223
+
أرسل
223
224
</button>
224
225
{status ==='error'&&
225
226
<p className="Error">
226
-
Good guess but a wrong answer. Try again!
227
+
توقع جيد ولكن إجابة خاطئة. حاول مرة أخرى!
227
228
</p>
228
229
}
229
230
</form>
@@ -240,9 +241,9 @@ export default function Form({
240
241
241
242
<DeepDive>
242
243
243
-
#### Displaying many visual states at once {/*displaying-many-visual-states-at-once*/}
244
+
#### عرض عديد من الحالات المرئية مرة واحدة {/*displaying-many-visual-states-at-once*/}
244
245
245
-
If a component has a lot of visual states, it can be convenient to show them all on one page:
246
+
لو أن لمكون العديد من الحالات المرئية، قد يكون ملائما عرضها جميعها في صفحة واحدة:
246
247
247
248
<Sandpack>
248
249
@@ -262,7 +263,7 @@ export default function App() {
262
263
<>
263
264
{statuses.map(status=> (
264
265
<section key={status}>
265
-
<h4>Form ({status}):</h4>
266
+
<h4>نموذج ({status}):</h4>
266
267
<Form status={status} />
267
268
</section>
268
269
))}
@@ -274,7 +275,7 @@ export default function App() {
274
275
```js Form.js
275
276
exportdefaultfunctionForm({ status }) {
276
277
if (status ==='success') {
277
-
return<h1>That's right!</h1>
278
+
return<h1>هذا صحيح!</h1>
278
279
}
279
280
return (
280
281
<form>
@@ -286,11 +287,11 @@ export default function Form({ status }) {
286
287
status ==='empty'||
287
288
status ==='submitting'
288
289
}>
289
-
Submit
290
+
أرسل
290
291
</button>
291
292
{status ==='error'&&
292
293
<p className="Error">
293
-
Good guess but a wrong answer. Try again!
294
+
توقع جيد ولكن إجابة خاطئة. حاول مرة أخرى!
294
295
</p>
295
296
}
296
297
</form>
@@ -307,7 +308,7 @@ body { margin: 0; }
307
308
308
309
</Sandpack>
309
310
310
-
Pages like this are often called "living styleguides" or "storybooks".
311
+
صفحات مثل هذه غالبًا يطلق عليها "living styleguides" أو "storybooks"
0 commit comments