Skip to content

Commit 1073bff

Browse files
authored
Merge pull request #84 from galnir/hooks-rules
translate hooks rules page
2 parents 479f9dd + 138ad49 commit 1073bff

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

content/docs/hooks-rules.md

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,73 @@
11
---
22
id: hooks-rules
3-
title: Rules of Hooks
3+
title: חוקי Hooks
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+
*Hooks* הם תוספת חדשה ב- React 16.8. הם נותנים לנו להשתמש ב- state ופיצ'רים אחרים של ריאקט מבלי לכתוב מחלקה.
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+
Hooks הם פונקציות ג'אווהסקריפט, אבל אתה צריך לנהוג על פי שני חוקים כשאתה משתמש בהם. אנו מספקים [פלאגין linting](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+
### ניתן לקרוא ל- Hooks רק ברמה העליונה {#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+
**אל תקרא ל- Hooks בתוך לופים, conditions כמו if או פונקציות מקוננות.** במקום זאת, תמיד השתמש ב- Hooks ברמה העליונה של פונקציית הריאקט שלך. על ידי ביצוע כלל זה, אתה מבטיח ש- Hooks נקראים באותו סדר כל פעם שקומפוננטה מתרנדרת. זה מה שמאפשר לריאקט לשמור את ה- state של Hooks בין קריאות מרובות של `useState` ו- `useEffect`. (אם אתה סקרן, נסביר זאת לעומק [למטה](#explanation).)
1616

17-
### Only Call Hooks from React Functions {#only-call-hooks-from-react-functions}
17+
###ניתן לקרוא ל- Hooks רק מתוך פונקציות ריאקט{#only-call-hooks-from-react-functions}
1818

19-
**Don't call Hooks from regular JavaScript functions.** Instead, you can:
19+
**אל תקרא ל- Hooks מתוך פונקציות ג'אווהסקריפט רגילות.** במקום זאת, אתה יכול:
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+
*לקרוא ל- Hooks מתוך קומפוננטות פונקציה של ריאקט.
22+
*לקרוא ל- Hooks מתוך Hooks מותאמים אישית( נלמד עליהם [בעמוד הבא](/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 בתוך קומפוננטה היא ברורה לעין מקוד המקור שלה.
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 שנקרא [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks) האוכף אוטומטית שני חוקים אלה. אתה יכול להוסיף את הפלאגין הזה לפרויקט שלך אם תרצה לנסות אותו:
2929

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

3434
```js
35-
// Your ESLint configuration
35+
// קונפיגורציית ה- ESLint שלך
3636
{
3737
"plugins": [
3838
// ...
3939
"react-hooks"
4040
],
4141
"rules": {
4242
// ...
43-
"react-hooks/rules-of-hooks": "error", // Checks rules of Hooks
44-
"react-hooks/exhaustive-deps": "warn" // Checks effect dependencies
43+
"react-hooks/rules-of-hooks": "error", // בודק חוקי Hooks
44+
"react-hooks/exhaustive-deps": "warn" // בודק effect dependencies
4545
}
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 וערכות כלים דומות.
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+
**אתה יכול לדלג לעמוד הבא שמסביר איך לכתוב [Hooks משלך](/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), אנחנו יכולים להשתמש במספר State או Effect Hooks בתוך קומפוננטה יחידה:
5656

5757
```js
5858
function Form() {
59-
// 1. Use the name state variable
59+
// 1. שימוש במשתנה name ב- state
6060
const [name, setName] = useState('Mary');
6161

6262
// 2. Use an effect for persisting the form
6363
useEffect(function persistForm() {
6464
localStorage.setItem('formData', name);
6565
});
6666

67-
// 3. Use the surname state variable
67+
// 3. שימוש במשתנה surname ב- state
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,63 @@ 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+
אז איך ריאקט יודע איזה state מתאים לאיזו קריאת `useState`? התשובה היא **שריאקט מסתמך על סדר קריאת ה- Hooks.** הדוגמה שלנו עובדת בגלל שסדר קריאות ה- Hooks הוא אותו דבר בכל רינדור:
8080

8181
```js
8282
// ------------
83-
// First render
83+
// רינדור ראשון
8484
// ------------
85-
useState('Mary') // 1. Initialize the name state variable with 'Mary'
85+
useState('Mary') // 1. איתחול המשתנה name ב- state עם 'Mary'
8686
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. הוספת אפקט על מנת לעדכן את הכותרת
8989

9090
// -------------
9191
// Second render
9292
// -------------
93-
useState('Mary') // 1. Read the name state variable (argument is ignored)
93+
useState('Mary') // 1. קריאת המשתנה name ב- state (מתעלמים מהקלט)
9494
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. מחליפים את האפקט על מנת לעדכן את הכותרת
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+
כל עוד הסדר של קריאות Hook הוא שווה בין רינדורים, ריאקט יכול לצרף state מקומי עם כל אחד מהם. אבל מה קורה אם אנחנו שמים קריאת Hook (לדוגמה, האקפט `persistForm`) בתוך condition?
102102

103103
```js
104-
// 🔴 We're breaking the first rule by using a Hook in a condition
104+
// 🔴 אנחנו עוברים על החוק הראשון על ידי שימוש ב- Hook בתוך condition
105105
if (name !== '') {
106106
useEffect(function persistForm() {
107107
localStorage.setItem('formData', name);
108108
});
109109
}
110110
```
111111

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 משתנה:
113113

114114
```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). נכשל בלהחליף את האפקט
119119
```
120120

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 אחרי האחת שדילגנו עליה תזוז באחד, דבר שיוביל לבאגים.
122122

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 שלנו:
124124

125125
```js
126126
useEffect(function persistForm() {
127-
// 👍 We're not breaking the first rule anymore
127+
// 👍 אנחנו לא עוברים על החוק הראשון יותר
128128
if (name !== '') {
129129
localStorage.setItem('formData', name);
130130
}
131131
});
132132
```
133133

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 עובדים בדרך זו, ואילו בעיות החוק הזה מונע.
135135

136-
## Next Steps {#next-steps}
136+
## הצעדים הבאים {#next-steps}
137137

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

Comments
 (0)