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
In the past, JavaScript errors inside components used to corrupt React’s internal state and cause it to [emit](https://github.com/facebook/react/issues/4026)[cryptic](https://github.com/facebook/react/issues/6895)[errors](https://github.com/facebook/react/issues/8579)on next renders. These errors were always caused by an earlier error in the application code, but React did not provide a way to handle them gracefully in components, and could not recover from them.
7
+
בעבר, שגיאות בתוך קומפוננטות ב-JavaScript הובילו להשחתת המצב הפנימי של React וגרמו ל[פליטת](https://github.com/facebook/react/issues/4026)[שגיאות](https://github.com/facebook/react/issues/6895)[אניגמטיות](https://github.com/facebook/react/issues/8579)ברינדור המסך הבא. מקור הבעיה תמיד נבע משגיאות קודמות בקוד האפליקציה, אבל React לא סיפק דרך לטפל בהם בחן בתוך הקומפוננטות, ולא מצא דרך להתאושש מהם.
שגיאת JavaScript בחלק מממשק המשתמש לא אמורה לשבור את כל האפליקציה. כדי לפתור את הבעיה למשתמשי React, גרסה 16 מציגה קונספט חדש של ״גבולות שגיאה״.
11
12
12
-
A JavaScript error in a part of the UI shouldn’t break the whole app. To solve this problem for React users, React 16 introduces a new concept of an “error boundary”.
13
+
גבולות שגיאה הם בעצם קומפוננטות ש**תופסות שגיאות JavaScript שקורות בכל אחד מקומפוננטות הילד שלהן, מתעדות אותן ומציגות ממשק חלופי.** במקום להציג את הקומפוננטה השבורה. הן תופסות שגיאות בזמן רינדור, במתודות מחזור חיים ובבנאי הקומפוננטות עבור כל אחת מקומפוננטות הילד שלהן.
13
14
14
-
Error boundaries are React components that **catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI** instead of the component tree that crashed. Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.
A class component becomes an error boundary if it defines either (or both) of the lifecycle methods [`static getDerivedStateFromError()`](/docs/react-component.html#static-getderivedstatefromerror) or [`componentDidCatch()`](/docs/react-component.html#componentdidcatch). Use `static getDerivedStateFromError()` to render a fallback UI after an error has been thrown. Use `componentDidCatch()` to log error information.
24
+
קומפוננטת מחלקה הופכת לגבול שגיאה אם היא מגדירה לפחות אחת ממתודות מחזור החיים [`static getDerivedStateFromError()`](/docs/react-component.html#static-getderivedstatefromerror) או [`componentDidCatch()`](/docs/react-component.html#componentdidcatch).
25
+
המתודה `static getDerivedStateFromError()` משמשת לרנדור ממשק חלופי לאחר שגיאה שנתפסה, ו- `componentDidCatch()` עוזרת בתיעוד השגיאה.
26
26
27
27
```js{7-10,12-15,18-21}
28
28
class ErrorBoundary extends React.Component {
@@ -32,18 +32,18 @@ class ErrorBoundary extends React.Component {
32
32
}
33
33
34
34
static getDerivedStateFromError(error) {
35
-
// Update state so the next render will show the fallback UI.
35
+
// כדי שהרינדור הבא יציג ממשק חלופי state מעדכנת את ה
36
36
return { hasError: true };
37
37
}
38
38
39
39
componentDidCatch(error, info) {
40
-
// You can also log the error to an error reporting service
40
+
// אפשר גם לתעד את השגיאה לשירות לוגר
41
41
logErrorToMyService(error, info);
42
42
}
43
43
44
44
render() {
45
45
if (this.state.hasError) {
46
-
// You can render any custom fallback UI
46
+
// מגדירים ממשק חלופי מותאם
47
47
return <h1>Something went wrong.</h1>;
48
48
}
49
49
@@ -52,61 +52,60 @@ class ErrorBoundary extends React.Component {
52
52
}
53
53
```
54
54
55
-
Then you can use it as a regular component:
55
+
השימוש בגבולות שגיאה זהה לשימוש בכל קומפוננטה רגילה:
56
56
57
57
```js
58
58
<ErrorBoundary>
59
59
<MyWidget />
60
60
</ErrorBoundary>
61
61
```
62
62
63
-
Error boundaries work like a JavaScript `catch {}` block, but for components. Only class components can be error boundaries. In practice, most of the time you’ll want to declare an error boundary component once and use it throughout your application.
64
-
65
-
Note that **error boundaries only catch errors in the components below them in the tree**. An error boundary can’t catch an error within itself. If an error boundary fails trying to render the error message, the error will propagate to the closest error boundary above it. This, too, is similar to how catch {} block works in JavaScript.
63
+
גבולות שגיאה עובדים בצורה דומה לבלוק `catch {}` ב-JavaScript, אבל בתוך הקומפוננטה.
64
+
רק קומפוננטות מחלקה יכולות להיות גבולות שגיאה. בפועל, מגדירים בדרך כלל גבול שגיאה אחד ונשתמש בו בצורה אחידה בכל האפליקציה.
66
65
67
-
## Live Demo {#live-demo}
66
+
שימו לב ש**גבולות שגיאה תופסים אך ורק שגיאות בקומפוננטות הילד שלהם**, ולא בתוך עצמם. אם מתרחשת שגיאה בקוד ה- `render` של גבולות השגיאה לדוגמא, השגיאות תעלה לגבול שגיאה הבא מעליה, בדיוק כמצופה מההתנהגות של בלוק ה- `catch {}` ב-JavaScript.
68
67
69
-
Check out [this example of declaring and using an error boundary](https://codepen.io/gaearon/pen/wqvxGa?editors=0010) with [React 16](/blog/2017/09/26/react-v16.0.html).
68
+
## הדגמה חיה {#live-demo}
70
69
70
+
שימו לב ל[דוגמא הבאה של הגדרה ושימוש בגבולות שגיאה](https://codepen.io/gaearon/pen/wqvxGa?editors=0010) עם [גרסה 16 של React](/blog/2017/09/26/react-v16.0.html).
71
71
72
-
## Where to Place Error Boundaries {#where-to-place-error-boundaries}
73
72
74
-
The granularity of error boundaries is up to you. You may wrap top-level route components to display a “Something went wrong” message to the user, just like server-side frameworks often handle crashes. You may also wrap individual widgets in an error boundary to protect them from crashing the rest of the application.
73
+
## איפה למקם את גבולות שגיאה {#where-to-place-error-boundaries}
75
74
75
+
אפשר להגדיר גבולות שגיאה ברמה גבוהה או נמוכה לפי הצורך או לפי ההעדפה אישית. לדוגמא, בשורש הצומת של עץ הקומפוננטות עם ממשק חלופי של ״משהו השתבש..״ (כמו שלרוב מנוהלות התרסקויות בצד השרת), או לחלופין לעטוף כל קומפוננטה או ווידג׳ט בגבול שגיאה אישי כדי למנוע התרסקות של שאר האפליקציה.
76
76
77
-
## New Behavior for Uncaught Errors {#new-behavior-for-uncaught-errors}
77
+
## התנהגות חדשה לשגיאות שלא נתפסו {#new-behavior-for-uncaught-errors}
78
78
79
-
This change has an important implication. **As of React 16, errors that were not caught by any error boundary will result in unmounting of the whole React component tree.**
79
+
גרסה 16 של React מציגה שינוי בהתנהגות עם השלכות חשובות. **שגיאות של נתפסות על ידי גבולות שגיאה יביאו לפירוק מוחלט של עץ הקומפוננטות הראשי**.
80
80
81
-
We debated this decision, but in our experience it is worse to leave corrupted UI in place than to completely remove it. For example, in a product like Messenger leaving the broken UI visible could lead to somebody sending a message to the wrong person. Similarly, it is worse for a payments app to display a wrong amount than to render nothing.
81
+
דנו בהחלטה זו לא מעט, אבל מנסיונינו תמיד כדאי שלא להשתמש בממשק מושחת, אלא להפטר ממנו לגמרי. לדוגמא, באפליקציה כמו מסנג׳ר, להשאיר ממשק שבור בצורה גלויה לעין יכול להוביל לשליחת הודעה לאדם הלא נכון. באותה מידה, באפליקציה שמנהלת כספים עדיף לא להציג כלום מאשר להציג סכום שגוי.
82
82
83
-
This change means that as you migrate to React 16, you will likely uncover existing crashes in your application that have been unnoticed before. Adding error boundaries lets you provide better user experience when something goes wrong.
83
+
השינוי הזה אומר שכשמשדרגים לגרסה 16, בדרך כלל מגלים שגיאות באפליקציה שעד אז לא שמנו לב אליהן. הוספת גבולות שגיאה מאפשר לנו לספק חווית משתמש טובה יותר בכל מצב.
84
84
85
-
For example, Facebook Messenger wraps content of the sidebar, the info panel, the conversation log, and the message input into separate error boundaries. If some component in one of these UI areas crashes, the rest of them remain interactive.
85
+
לדוגמא, המסנג׳ר של Facebook עוטף תוכן מהסרגל הצידי, חלונית המידע, חלונית השיחה ושדה הקלט של ההודעה בגבולות שגיאה נפרדים. במקרה ואחד מהם קורס, האחרים נשארים זמינים ואינטרקטיביים.
86
86
87
-
We also encourage you to use JS error reporting services (or build your own) so that you can learn about unhandled exceptions as they happen in production, and fix them.
87
+
אנחנו ממליצים גם להשתמש בשירותי דיווח השגיאות של JavaScript (או לבנות שירותים דומים בעצמכם), על מנת למצוא בעיות בסביבת הייצור ולתקן אותן בקלות ובמהירות.
בגרסה 16 של React כל השגיאות שקורות בזמן הרינדור בסביבת הפיתוח מודפסות למסוף בדפדפן, אפילו אם האפליקציה בולעת אותם בטעות. בנוסף להודעת השגיאה ומעקב העירומת של JavaScript, מודפס גם מעקר הערימה של הקומפפוננטה. עכשיו אפשר לראות בדיוק איפה בקומפוננטה קרתה השגיאה:
91
92
92
-
React 16 prints all errors that occurred during rendering to the console in development, even if the application accidentally swallows them. In addition to the error message and the JavaScript stack, it also provides component stack traces. Now you can see where exactly in the component tree the failure has happened:
93
+
<imgsrc="../images/docs/error-boundaries-stack-trace.png"style="max-width:100%"alt="שיגאה שנתפסה על ידי גבולות שגיאה">
93
94
94
-
<imgsrc="../images/docs/error-boundaries-stack-trace.png"style="max-width:100%"alt="Error caught by Error Boundary component">
95
+
אפשר גם לראות את שם הקובץ ומספר השורה בקוד הקומפוננטה בעזרת מעקב הערימות. זה עובד בברירת המחדל בפרויקטים שנוצרו עם [אפליקצית Create React](https://github.com/facebookincubator/create-react-app):
95
96
96
-
You can also see the filenames and line numbers in the component stacktrace. This works by default in [Create React App](https://github.com/facebookincubator/create-react-app) projects:
97
+
<imgsrc="../images/docs/error-boundaries-stack-trace-line-numbers.png"style="max-width:100%"alt="שגיאה שנתפסה על ידי גבולות שגיאה עם מספר שורה">
97
98
98
-
<imgsrc="../images/docs/error-boundaries-stack-trace-line-numbers.png"style="max-width:100%"alt="Error caught by Error Boundary component with line numbers">
99
+
אם לא יצרתם את הפרויקט עם אפליקצית Create React, תוכלו להשתמש [בתוסף הזה](https://www.npmjs.com/package/babel-plugin-transform-react-jsx-source) - הוסיפו אותו לתצורת ה-Babel בפרויקט. שימו לב שהוא מיועד רק לשימוש בסביבת הפיתוח ו**חובה לנטרל אותו בסביבת הייצור**.
99
100
100
-
If you don’t use Create React App, you can add [this plugin](https://www.npmjs.com/package/babel-plugin-transform-react-jsx-source) manually to your Babel configuration. Note that it’s intended only for development and **must be disabled in production**.
101
-
102
-
> Note
101
+
> הערה
103
102
>
104
-
> Component names displayed in the stack traces depend on the [`Function.name`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name) property. If you support older browsers and devices which may not yet provide this natively (e.g. IE 11), consider including a `Function.name`polyfill in your bundled application, such as [`function.name-polyfill`](https://github.com/JamesMGreene/Function.name). Alternatively, you may explicitly set the [`displayName`](/docs/react-component.html#displayname)property on all your components.
103
+
> שמות הקומפוננטות שמוצגים במעקב הערימות תלוי בשם שהוגדר במאפיין [`Function.name`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name). אם אתם צריכים לתמוך בדפדפנים או מכשירים ישנים יותר שלא תומכים בזה באופן סטנדרטי (כמו IE 11 למשל), תוכלו להוסיף את המאפיין כ- polyfill שיוכלל ב-bundle האפליקציה, כמו [`function.name-polyfill`](https://github.com/JamesMGreene/Function.name). דרך נוספת היא לספק באופן ישיר את המאפיין [`displayName`](/docs/react-component.html#displayname)בכל קומפוננטה.
105
104
106
105
107
-
## How About try/catch? {#how-about-trycatch}
106
+
## מה עם בלוק try/catch? {#how-about-trycatch}
108
107
109
-
`try` / `catch`is great but it only works for imperative code:
108
+
בלוק `try/catch`זה מצוין אבל עובד רק בקוד אימפרטיבי (קוד שמשנה את מצב האפליקציה):
110
109
111
110
```js
112
111
try {
@@ -116,21 +115,22 @@ try {
116
115
}
117
116
```
118
117
119
-
However, React components are declarative and specify *what* should be rendered:
118
+
לעומת זאת, קומפוננטות React הן דקלרטיביות ורק מציינות *מה* צריך לרנדר באפליקציה:
120
119
121
120
```js
122
121
<Button />
123
122
```
124
123
125
-
Error boundaries preserve the declarative nature of React, and behave as you would expect. For example, even if an error occurs in a `componentDidUpdate` method caused by a `setState` somewhere deep in the tree, it will still correctly propagate to the closest error boundary.
124
+
גבולות שגיאה משמרים את האופן הדקלרטיבי של React ומספקים התנהגות דומה. לדוגמא, אפילו אם שגיאה צצה במתודת ה-`componentDidUpdate` שנגרמה איפשהו עמוק בתוך העץ בתוך `setState`, היא תוצף לגבול השגיאה הקרוב ביותר.
125
+
126
+
## מה עם מטפלי אירועים? {#how-about-event-handlers}
126
127
127
-
## How About Event Handlers? {#how-about-event-handlers}
128
+
גבולות שגיאה **לא** תופסים שגיאות מתוך מטפלי אירועים.
React לא צריך גבולות שגיאה כדי להתאושש משגיאות במטפלי אירועים. בשונה מהמתודת הרינדור ומתודות מחזור החיים, שגיאות שצצות במטפלי האירועים לא קורות בזמן הרינדור. אז אם צצה שגיאה, React עדיין ידע מה להציג.
130
131
131
-
React doesn't need error boundaries to recover from errors in event handlers. Unlike the render method and lifecycle methods, the event handlers don't happen during rendering. So if they throw, React still knows what to display on the screen.
132
+
כשיש צורך לתפוס שגיאה במטפל האירועים, השתמשו בבלוק ה-`try` / `catch` כרגיל:
132
133
133
-
If you need to catch an error inside event handler, use the regular JavaScript `try` / `catch` statement:
134
134
135
135
```js{9-13,17-20}
136
136
class MyComponent extends React.Component {
@@ -142,7 +142,7 @@ class MyComponent extends React.Component {
142
142
143
143
handleClick() {
144
144
try {
145
-
// Do something that could throw
145
+
// קוד שזורק שגיאה
146
146
} catch (error) {
147
147
this.setState({ error });
148
148
}
@@ -157,10 +157,10 @@ class MyComponent extends React.Component {
157
157
}
158
158
```
159
159
160
-
Note that the above example is demonstrating regular JavaScript behavior and doesn't use error boundaries.
160
+
שימו לב שהדוגמא הנ״ל מדגימה קוד JavaScript סטנדרטי לטיפול בשגיאות ולא קשורה בשום אופן לגבולות שגיאות.
161
161
162
-
## Naming Changes from React 15 {#naming-changes-from-react-15}
162
+
## שינוי שם מגרסה 15 {#naming-changes-from-react-15}
163
163
164
-
React 15 included a very limited support for error boundaries under a different method name: `unstable_handleError`. This method no longer works, and you will need to change it to `componentDidCatch`in your code starting from the first 16 beta release.
164
+
גרסה 15 של React כללה תמיכה מוגבלת ביותר לגבולות שגיאות תחת מתודה בשם אחר: `unstable_handleError`. המתודה הזאת כבר לא נתמכת, ותאלצו לשנות אותה ל- `componentDidCatch`בקוד שלכם החל מגרסת הביתא הראשונה של React 16.
165
165
166
-
For this change, we’ve provided a [codemod](https://github.com/reactjs/react-codemod#error-boundaries)to automatically migrate your code.
166
+
בשביל השינוי הזה, כללנו [משנה קוד (codemod)](https://github.com/reactjs/react-codemod#error-boundaries)כדי לעדכן באופן אוטומטי את הקוד הרלוונטי.
0 commit comments