Skip to content

Commit be28851

Browse files
authored
Merge pull request #61 from splimter/Translate-hooks-rules.md
Done Translate-hooks-rules.md
2 parents 8db1920 + 89a337f commit be28851

File tree

1 file changed

+46
-45
lines changed

1 file changed

+46
-45
lines changed

content/docs/hooks-rules.md

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
---
22
id: hooks-rules
3-
title: Rules of Hooks
3+
title: قواعد استعمال الخطافات في React
44
permalink: docs/hooks-rules.html
55
next: hooks-custom.html
66
prev: hooks-effect.html
77
---
88

9-
*Hooks* are a new addition in React 16.8. They let you use state and other React features without writing a class.
9+
*الخطافات* هي إضافة جديدة إلى الإصدار 16.8 في React، إذ تسمح لك باستعمال ميزة الحالة وميزات React الأخرى دون كتابة أي صنف:
1010

11-
Hooks are JavaScript functions, but you need to follow two rules when using them. We provide a [linter plugin](https://www.npmjs.com/package/eslint-plugin-react-hooks) to enforce these rules automatically:
11+
الخطافات هي دوال JavaScript، ولكن تحتاج إلى اتباع قاعدتين عند استعمالها. نوفر [إضافة تصحيح أخطاء الصياغة](https://www.npmjs.com/package/eslint-plugin-react-hooks) تجبرك على تطبيق هاتين القاعدتين تلقائيًّا.
1212

13-
### Only Call Hooks at the Top Level {#only-call-hooks-at-the-top-level}
13+
### استدعي الخطافات في المستوى الأعلى فقط {#only-call-hooks-at-the-top-level}
1414

15-
**Don't call Hooks inside loops, conditions, or nested functions.** Instead, always use Hooks at the top level of your React function. By following this rule, you ensure that Hooks are called in the same order each time a component renders. That's what allows React to correctly preserve the state of Hooks between multiple `useState` and `useEffect` calls. (If you're curious, we'll explain this in depth [below](#explanation).)
15+
**لا تستدعي الخطافات داخل حلقات التكرار، أو التعابير الشرطية، أو الدوال المتشعبة.** بدلًا من ذلك، استعمل الخطافات دومًا في المستوى الأعلى (top level) من دالة React. بتطبيق هذه القاعدة، تتأكد من أن الخطافات تُستدَعى بالترتيب نفسه في كل مرة يصيَّر فيها مكونٌ. هذا يسمح لـ React بحفظ حالة الخطافات بين الاستدعاءات `useState` و `useEffect` المتعددة بشكل صحيح. (إن شدَّك فضولك للتعلم أكثر، سنشرح هذا الأمر بالتفصيل في [الأسفل](#explanation).)
1616

17-
### Only Call Hooks from React Functions {#only-call-hooks-from-react-functions}
17+
### استدعي الخطافات من دوال React فقط {#only-call-hooks-from-react-functions}
1818

19-
**Don't call Hooks from regular JavaScript functions.** Instead, you can:
19+
**لا تستدعي الخطافات من دوال JavaScript العادية.** بدلًا من ذلك، يمكنك:
2020

21-
*Call Hooks from React function components.
22-
*Call Hooks from custom Hooks (we'll learn about them [on the next page](/docs/hooks-custom.html)).
21+
*استدعاء الخطافات من مكونات دالة React.
22+
*استدعاء الخطافات من خطافات مخصصة (سنتعرف عليهم في [الصفحة التالية](/docs/hooks-custom.html)).
2323

24-
By following this rule, you ensure that all stateful logic in a component is clearly visible from its source code.
24+
باتباع هذه القاعدة، تتأكد من أنَّ الشيفرة ذات الحالة (stateful logic) في مكونٍ ما مرئيةٌ بوضوح من شيفرتها المصدرية.
2525

26-
## ESLint Plugin {#eslint-plugin}
26+
## الإضافة ESLint {#eslint-plugin}
2727

28-
We released an ESLint plugin called [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks) that enforces these two rules. You can add this plugin to your project if you'd like to try it:
28+
أطلقنا إضافةً لتصحيح الأخطاء تدعى [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks) تجبر على تطبيق هاتين القاعدتين. يمكنك إضافة هذه الإضافة إلى مشروعك إن أحببت تجريبها:
2929

3030
```bash
3131
npm install eslint-plugin-react-hooks
3232
```
3333

3434
```js
35-
// Your ESLint configuration
35+
// الخاص بك ESLint ضبط الإضافة
3636
{
3737
"plugins": [
3838
// ...
@@ -46,28 +46,28 @@ npm install eslint-plugin-react-hooks
4646
}
4747
```
4848

49-
In the future, we intend to include this plugin by default into Create React App and similar toolkits.
49+
في المستقبل، ننوي تضمين هذه الإضافة افتراضيًّا عند بناء تطبيقات جديدة باستعمال الأمر Create React App أو أية مجموعة أدوات أخرى (toolkits).
5050

51-
**You can skip to the next page explaining how to write [your own Hooks](/docs/hooks-custom.html) now.** On this page, we'll continue by explaining the reasoning behind these rules.
51+
**يمكنك تخطي بقية هذه الصفحة إلى الصفحة التالية: [بناء خطافات خاصة بك](/docs/hooks-custom.html) الآن.** إذ سنكمل شرح سبب وجود هاتين القاعدتين.
5252

53-
## Explanation {#explanation}
53+
## الشرح {#explanation}
5454

55-
As we [learned earlier](/docs/hooks-state.html#tip-using-multiple-state-variables), we can use multiple State or Effect Hooks in a single component:
55+
كما [تعلمنا سابقًا](/docs/hooks-state.html#tip-using-multiple-state-variables), يمكننا استعمال خطافات حالة أو تأثير متعددة في مكون واحد:
5656

5757
```js
5858
function Form() {
59-
// 1. Use the name state variable
59+
// 1. نفسه name استعمال متغير الحالة
6060
const [name, setName] = useState('Mary');
6161

62-
// 2. Use an effect for persisting the form
62+
// 2. استعمال تأثير من أجل استمرار النموذج
6363
useEffect(function persistForm() {
6464
localStorage.setItem('formData', name);
6565
});
6666

67-
// 3. Use the surname state variable
67+
// 3. surname استعمال متغير الحالة
6868
const [surname, setSurname] = useState('Poppins');
6969

70-
// 4. Use an effect for updating the title
70+
// 4. استعمال تأثير من أجل تحديث العنوان
7171
useEffect(function updateTitle() {
7272
document.title = name + ' ' + surname;
7373
});
@@ -76,63 +76,64 @@ function Form() {
7676
}
7777
```
7878

79-
So how does React know which state corresponds to which `useState` call? The answer is that **React relies on the order in which Hooks are called**. Our example works because the order of the Hook calls is the same on every render:
79+
إذًا، كيف تعرف React أية حالة توافق أي استدعاء للخطاف `useState`؟ الجواب هو أنَّ **React تعتمد على الترتيب الذي استدعيت الخطافات به**. مثالنا السابق يعمل بشكل صحيح لأنَّ ترتيب استدعاء الخطافات هو نفسه في كل تصيير:
8080

8181
```js
8282
// ------------
83-
// First render
83+
// أول تصيير
8484
// ------------
85-
useState('Mary') // 1. Initialize the name state variable with 'Mary'
86-
useEffect(persistForm) // 2. Add an effect for persisting the form
87-
useState('Poppins') // 3. Initialize the surname state variable with 'Poppins'
88-
useEffect(updateTitle) // 4. Add an effect for updating the title
85+
useState('Mary') // 1. 'Mary' إلى name ضبط متغير الحالة
86+
useEffect(persistForm) // 2. persistForm إضافى تأثير من أجل استمرار النموذج
87+
useState('Poppins') // 3. 'Poppins' إلى surname ضبط متغير الحالة
88+
useEffect(updateTitle) // 4. إضافة تأثير من أجل تحديث العنوان
8989

9090
// -------------
91-
// Second render
91+
// ثاني تصيير
9292
// -------------
93-
useState('Mary') // 1. Read the name state variable (argument is ignored)
94-
useEffect(persistForm) // 2. Replace the effect for persisting the form
95-
useState('Poppins') // 3. Read the surname state variable (argument is ignored)
96-
useEffect(updateTitle) // 4. Replace the effect for updating the title
93+
useState('Mary') // 1. (يتجاهل الوسيط) name قراءة متغير الحالة
94+
useEffect(persistForm) // 2. استبدال التأثير من أجل استمرار النموذج
95+
useState('Poppins') // 3. (يتجاهل الوسيط) surname قراءة متغير الحالة
96+
useEffect(updateTitle) // 4. استبدال التأثير من أجل تحديث العنوان
9797

9898
// ...
9999
```
100100

101-
As long as the order of the Hook calls is the same between renders, React can associate some local state with each of them. But what happens if we put a Hook call (for example, the `persistForm` effect) inside a condition?
101+
طالمًا أنَّ ترتيب استدعاءات الخطاف هو نفسه بين عمليات التصيير، تستطيع React من ربط بعض الحالات المحلية مع بعضها بعضًا. ولكن، ماذا يحصل إن وضعنا استدعاء خطاف (التأثير `persistForm` مثلًا) داخل تعبير شرطي؟
102102

103103
```js
104-
// 🔴 We're breaking the first rule by using a Hook in a condition
104+
// 🔴 نكسر الآن القاعدة الأولى عبر استعمال خطاف داخل تعبير شرطي
105+
105106
if (name !== '') {
106107
useEffect(function persistForm() {
107108
localStorage.setItem('formData', name);
108109
});
109110
}
110111
```
111112

112-
The `name !== ''` condition is `true` on the first render, so we run this Hook. However, on the next render the user might clear the form, making the condition `false`. Now that we skip this Hook during rendering, the order of the Hook calls becomes different:
113+
الشرط `name !== ''` هو `true` في عملية التصيير الأولى، لذا يُنفَّذ هذا الخطاف. مع ذلك، في عملية التصيير التالية، قد يمسح المستخدم النموذج مما يؤدي إلى تغيِّر قيمة الشرط إلى `false`. الآن وبعد أن تخطينا هذا الخطاف أثناء عملية التصيير، أصبح ترتيب استدعاءات الخطاف مختلفًا:
113114

114115
```js
115-
useState('Mary') // 1. Read the name state variable (argument is ignored)
116-
// useEffect(persistForm) // 🔴 This Hook was skipped!
117-
useState('Poppins') // 🔴 2 (but was 3). Fail to read the surname state variable
118-
useEffect(updateTitle) // 🔴 3 (but was 4). Fail to replace the effect
116+
useState('Mary') // 1. (يتجاهل الوسيط) name قراءة متغير الحالة
117+
// useEffect(persistForm) // 🔴 يُتخطَى هذا الخطاف
118+
useState('Poppins') // 🔴 2 (يجب أن تكون 3) surname فشل قراءة متغير الحالة
119+
useEffect(updateTitle) // 🔴 3 (فشل استبدال التأثير (يجب أن تكون 4
119120
```
120121

121-
React wouldn't know what to return for the second `useState` Hook call. React expected that the second Hook call in this component corresponds to the `persistForm` effect, just like during the previous render, but it doesn't anymore. From that point, every next Hook call after the one we skipped would also shift by one, leading to bugs.
122+
لن تعرف React ما الذي ستعيده من أجل استدعاء الخطاف `useState` الثاني. تتوقع React أن استدعاء الخطاف الثاني في هذا المكون يقابل التأثير `persistForm` مثل عملية التصيير السابقة، ولكن الحالة الآن لم تعد مشابه لها. بدءًا من تلك النقطة، كل استدعاء خطاف لاحق بعد ذلك الخطاف الذي جرى تخطيه سينزاح مرةً واحدةً مما يؤدي إلى حصول أخطاء.
122123

123-
**This is why Hooks must be called on the top level of our components.** If we want to run an effect conditionally, we can put that condition *inside* our Hook:
124+
**هذا هو سبب وجوب استدعاء الخطافات في المستوى الأعلى من مكوناتنا.** إن أردنا تنفيذ تأثير شرطيًّا، يمكننا وضع هذا الشرط *داخل* خطافنا:
124125

125126
```js
126127
useEffect(function persistForm() {
127-
// 👍 We're not breaking the first rule anymore
128+
// 👍 لم نخرق القاعدة الأولى
128129
if (name !== '') {
129130
localStorage.setItem('formData', name);
130131
}
131132
});
132133
```
133134

134-
**Note that you don't need to worry about this problem if you use the [provided lint rule](https://www.npmjs.com/package/eslint-plugin-react-hooks).** But now you also know *why* Hooks work this way, and which issues the rule is preventing.
135+
**لاحظ أنَّه لا داعي للقلق حول هذه المشكلة إن كنت تستعمل [إضافة التصحيح](https://www.npmjs.com/package/eslint-plugin-react-hooks).** الذي أشرنا إليها في الأعلى. على أية، أصبحت الآن تعرف سبب عمل الخطافات بهذه الطريقة، وما هي المشكلات التي تمنع القاعدة من حدوثها.
135136

136-
## Next Steps {#next-steps}
137+
## الخطوات التالية {#next-steps}
137138

138-
Finally, we're ready to learn about [writing your own Hooks](/docs/hooks-custom.html)! Custom Hooks let you combine Hooks provided by React into your own abstractions, and reuse common stateful logic between different components.
139+
أخيرًا، أصبحنا جاهزين لنتعلم كيفية [كتابة خطافات مخصصة](/docs/hooks-custom.html)! تخدم الغرض الذي نريد. الخطافات المخصصة تمكِّننا من دمج الخطافات التي توفرها React سويةً، وإعادة استعمال شيفرة شائعة ذات حالة بين مختلف المكونات.

0 commit comments

Comments
 (0)