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
*Hooks*are a new addition in React 16.8. They let you use state and other React features without writing a class.
9
+
*Hooks*הם תוספת חדשה ב- React 16.8. הם נותנים לנו להשתמש ב- state ופיצ'רים אחרים של ריאקט מבלי לכתוב מחלקה.
10
10
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
+
Hooks הם פונקציות ג'אווהסקריפט, אבל אתה צריך לנהוג על פי שני חוקים כשאתה משתמש בהם. אנו מספקים [פלאגין linting](https://www.npmjs.com/package/eslint-plugin-react-hooks)שאוכף חוקים אלו אוטומטית:
12
12
13
-
### Only Call Hooks at the Top Level {#only-call-hooks-at-the-top-level}
13
+
### ניתן לקרוא ל- Hooks רק ברמה העליונה {#only-call-hooks-at-the-top-level}
14
14
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
+
**אל תקרא ל- Hooks בתוך לופים, conditions כמו if או פונקציות מקוננות.**במקום זאת, תמיד השתמש ב- Hooks ברמה העליונה של פונקציית הריאקט שלך. על ידי ביצוע כלל זה, אתה מבטיח ש- Hooks נקראים באותו סדר כל פעם שקומפוננטה מתרנדרת. זה מה שמאפשר לריאקט לשמור את ה- state של Hooks בין קריאות מרובות של `useState`ו-`useEffect`. (אם אתה סקרן, נסביר זאת לעומק [למטה](#explanation).)
16
16
17
-
###Only Call Hooks from React Functions {#only-call-hooks-from-react-functions}
17
+
###ניתן לקרוא ל- Hooks רק מתוך פונקציות ריאקט{#only-call-hooks-from-react-functions}
18
18
19
-
**Don't call Hooks from regular JavaScript functions.**Instead, you can:
19
+
**אל תקרא ל- Hooks מתוך פונקציות ג'אווהסקריפט רגילות.**במקום זאת, אתה יכול:
20
20
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
+
* ✅ לקרוא ל- Hooks מתוך קומפוננטות פונקציה של ריאקט.
22
+
* ✅ לקרוא ל- Hooks מתוך Hooks מותאמים אישית( נלמד עליהם [בעמוד הבא](/docs/hooks-custom.html)).
23
23
24
-
By following this rule, you ensure that all stateful logic in a component is clearly visible from its source code.
24
+
על ידי ביצוע כלל זה, אתה מבטיח שכל לוגיקה שהיא stateful בתוך קומפוננטה היא ברורה לעין מקוד המקור שלה.
25
25
26
-
## ESLint Plugin {#eslint-plugin}
26
+
## פלאגין ESLint {#eslint-plugin}
27
27
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 שנקרא [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks)האוכף אוטומטית שני חוקים אלה. אתה יכול להוסיף את הפלאגין הזה לפרויקט שלך אם תרצה לנסות אותו:
29
29
30
30
```bash
31
31
npm install eslint-plugin-react-hooks --save-dev
32
32
```
33
33
34
34
```js
35
-
//Your ESLint configuration
35
+
//קונפיגורציית ה- ESLint שלך
36
36
{
37
37
"plugins": [
38
38
// ...
39
39
"react-hooks"
40
40
],
41
41
"rules": {
42
42
// ...
43
-
"react-hooks/rules-of-hooks":"error", //Checks rules of Hooks
In the future, we intend to include this plugin by default into Create React App and similar toolkits.
49
+
בעתיד, אנחנו מתכוונים לכלול את פלאגין זה בתוך Create React App וערכות כלים דומות.
50
50
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
+
**אתה יכול לדלג לעמוד הבא שמסביר איך לכתוב [Hooks משלך](/docs/hooks-custom.html)עכשיו.**בעמוד זה, נמשיך בלהסביר את הסיבות שמאחורי חוקים אלה.
52
52
53
-
## Explanation {#explanation}
53
+
## הסבר {#explanation}
54
54
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), אנחנו יכולים להשתמש במספר State או Effect Hooks בתוך קומפוננטה יחידה:
56
56
57
57
```js
58
58
functionForm() {
59
-
// 1. Use the name state variable
59
+
// 1. שימוש במשתנה name ב- state
60
60
const [name, setName] =useState('Mary');
61
61
62
62
// 2. Use an effect for persisting the form
63
63
useEffect(functionpersistForm() {
64
64
localStorage.setItem('formData', name);
65
65
});
66
66
67
-
// 3. Use the surname state variable
67
+
// 3. שימוש במשתנה surname ב- state
68
68
const [surname, setSurname] =useState('Poppins');
69
69
70
-
// 4. Use an effect for updating the title
70
+
// 4. שימוש באפקט על מנת לעדכן את הכותרת
71
71
useEffect(functionupdateTitle() {
72
72
document.title= name +''+ surname;
73
73
});
@@ -76,63 +76,63 @@ function Form() {
76
76
}
77
77
```
78
78
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
+
אז איך ריאקט יודע איזה state מתאים לאיזו קריאת`useState`? התשובה היא **שריאקט מסתמך על סדר קריאת ה- Hooks.** הדוגמה שלנו עובדת בגלל שסדר קריאות ה- Hooks הוא אותו דבר בכל רינדור:
80
80
81
81
```js
82
82
// ------------
83
-
//First render
83
+
//רינדור ראשון
84
84
// ------------
85
-
useState('Mary') // 1. Initialize the name state variable with 'Mary'
85
+
useState('Mary') // 1. איתחול המשתנה name ב- state עם 'Mary'
86
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
87
+
useState('Poppins') // 3. איתחול המשתנה surname ב- state עם 'Poppins'
88
+
useEffect(updateTitle) // 4. הוספת אפקט על מנת לעדכן את הכותרת
89
89
90
90
// -------------
91
91
// Second render
92
92
// -------------
93
-
useState('Mary') // 1. Read the name state variable (argument is ignored)
93
+
useState('Mary') // 1. קריאת המשתנה name ב- state (מתעלמים מהקלט)
94
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
95
+
useState('Poppins') // 3. קוראים את המשתנה surname ב- state (מתעלמים מהקלט)
96
+
useEffect(updateTitle) // 4. מחליפים את האפקט על מנת לעדכן את הכותרת
97
97
98
98
// ...
99
99
```
100
100
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
+
כל עוד הסדר של קריאות Hook הוא שווה בין רינדורים, ריאקט יכול לצרף state מקומי עם כל אחד מהם. אבל מה קורה אם אנחנו שמים קריאת Hook (לדוגמה, האקפט`persistForm`) בתוך condition?
102
102
103
103
```js
104
-
// 🔴 We're breaking the first rule by using a Hook in a condition
104
+
// 🔴 אנחנו עוברים על החוק הראשון על ידי שימוש ב- Hook בתוך condition
105
105
if (name !=='') {
106
106
useEffect(functionpersistForm() {
107
107
localStorage.setItem('formData', name);
108
108
});
109
109
}
110
110
```
111
111
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:
112
+
ה- condition `name !== ''`הוא `true`ברינדור הראשון, אז אנחנו מריצים Hook זה. למרות זאת, יכול להיות שברינדור הבא המשתמש ינקה את הטופס, וכתוצאה מכך ה- condition יהפוך ל- `false`. עכשיו שאנחנו מדלגים על Hook זה בזמן רינדור, הסדר של קריאות Hook משתנה:
113
113
114
114
```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
115
+
useState('Mary') // 1. קריאת המשתנה name ב- state (מתעלמים מהקלט)
116
+
// useEffect(persistForm) // 🔴 דילגנו על Hook זה!
117
+
useState('Poppins') // 🔴 2 (אבל היה 3). נכשל בלקרוא את המשתנה surname
118
+
useEffect(updateTitle) // 🔴 3 (אבל היה 4). נכשל בלהחליף את האפקט
119
119
```
120
120
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.
121
+
ריאקט לא ידע מה להחזיר לקריאה השנייה של ה- Hook `useState`. ריאקט ציפה שקריאת ה- Hook השנייה בקומפוננטה הזו תהיה תואמת לאפקט `persistForm`, בדיוק כמו ברינדור הקודם, אבל זה לא דומה יותר. מנקודה זו, כל קריאת Hook אחרי האחת שדילגנו עליה תזוז באחד, דבר שיוביל לבאגים.
122
122
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:
123
+
**זאת הסיבה שחייב לקרוא ל- Hooks ברמה העליונה של הקומפוננטות שלנו.**אם אנחנו רוצים להריץ אפקט מותנה, אנחנו יכולים לשים תנאי זה *בתוך* ה- Hook שלנו:
124
124
125
125
```js
126
126
useEffect(functionpersistForm() {
127
-
// 👍 We're not breaking the first rule anymore
127
+
// 👍 אנחנו לא עוברים על החוק הראשון יותר
128
128
if (name !=='') {
129
129
localStorage.setItem('formData', name);
130
130
}
131
131
});
132
132
```
133
133
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.
134
+
**שים לב שאתה לא צריך לדאוג מבעיה זו אם אתה משתמש [בחוק lint](https://www.npmjs.com/package/eslint-plugin-react-hooks).**אבל עכשיו אתה גם יודע *למה* Hooks עובדים בדרך זו, ואילו בעיות החוק הזה מונע.
135
135
136
-
## Next Steps {#next-steps}
136
+
## הצעדים הבאים {#next-steps}
137
137
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.
138
+
סוף כל סוף, אנו מוכנים ללמוד על כתיבת [Hooks משלנו](/docs/hooks-custom.html)! Hooks מותאמים אישית נותנים לך לשלב Hooks שמסופקים על ידי ריאקט לתוך האבסטרקציות שלך, ולעשות שימוש חוזר בלוגיקה שהיא stateful בין קומפוננטות שונות.
0 commit comments