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: content/docs/conditional-rendering.md
+37-37Lines changed: 37 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,30 +1,30 @@
1
1
---
2
2
id: conditional-rendering
3
-
title: Conditional Rendering
3
+
title: רינדור מותנה
4
4
permalink: docs/conditional-rendering.html
5
5
prev: handling-events.html
6
6
next: lists-and-keys.html
7
7
redirect_from:
8
8
- "tips/false-in-jsx.html"
9
9
---
10
10
11
-
In React, you can create distinct components that encapsulate behavior you need. Then, you can render only some of them, depending on the state of your application.
11
+
ב-React, אתם יכולים ליצור קומפוננטות יחודיות אשר מכמסות את ההתנהגות שאתם מחפשים. לאחר מכן, אתם יכולים לרנדר רק חלק מהן, על פי תלות ב-state של האפליקציה שלכם.
12
12
13
-
Conditional rendering in React works the same way conditions work in JavaScript. Use JavaScript operators like [`if`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else)or the [conditional operator](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)to create elements representing the current state, and let React update the UI to match them.
13
+
רינדור מותנה ב-React פועל באותו אופן שבו עובדים תנאים ב-JavaScript. השתמשו באופרטורים של JavaScript כמו [`if`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else)או [האופרטו המותנה](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)כדי ליצור אלמנטים המייצגים את ה-state הנוכחי, ותנו ל-React לעדכן את ממשק המשתמש כדי שיהיה תואם אליהם.
14
14
15
-
Consider these two components:
15
+
הביטו בשתי הקומפוננטות האלו:
16
16
17
17
```js
18
18
functionUserGreeting(props) {
19
-
return<h1>Welcome back!</h1>;
19
+
return<h1>ברוך הבא!</h1>;
20
20
}
21
21
22
22
functionGuestGreeting(props) {
23
-
return<h1>Please sign up.</h1>;
23
+
return<h1>אנא הירשם.</h1>;
24
24
}
25
25
```
26
26
27
-
We'll create a `Greeting`component that displays either of these components depending on whether a user is logged in:
27
+
ניצור קומפוננטת `Greeting`שמציגה אחת מהקומפוננטות האלו כתלות באם משתמש מחובר:
28
28
29
29
```javascript{3-7,11,12}
30
30
function Greeting(props) {
@@ -42,37 +42,37 @@ ReactDOM.render(
42
42
);
43
43
```
44
44
45
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/ZpVxNq?editors=0011)
45
+
[**נסו זאת ב-CodePen**](https://codepen.io/gaearon/pen/ZpVxNq?editors=0011)
46
46
47
-
This example renders a different greeting depending on the value of `isLoggedIn` prop.
47
+
דוגמא זו מרנדרת ברכה שונה כתלות בערך של ה-prop `isLoggedIn`.
48
48
49
-
### Element Variables {#element-variables}
49
+
### משתני אלמנט {#element-variables}
50
50
51
-
You can use variables to store elements. This can help you conditionally render a part of the component while the rest of the output doesn't change.
51
+
אתם יכולים להשתמש במשתנים כדי לשמור אלמנטים. זה יכול לעזור לכם לרנדר חלק מהקומפוננטה באופן מותנה בעוד ששאר הפלט אינו משתנה.
52
52
53
-
Consider these two new components representing Logout and Login buttons:
53
+
הביטו בשתי הקומפוננטות החדשות המייצגות כפתורי התנתקות והתחברות:
54
54
55
55
```js
56
56
functionLoginButton(props) {
57
57
return (
58
58
<button onClick={props.onClick}>
59
-
Login
59
+
התחבר
60
60
</button>
61
61
);
62
62
}
63
63
64
64
functionLogoutButton(props) {
65
65
return (
66
66
<button onClick={props.onClick}>
67
-
Logout
67
+
התנתק
68
68
</button>
69
69
);
70
70
}
71
71
```
72
72
73
-
In the example below, we will create a [stateful component](/docs/state-and-lifecycle.html#adding-local-state-to-a-class)called`LoginControl`.
It will render either `<LoginButton />`or`<LogoutButton />`depending on its current state. It will also render a `<Greeting />`from the previous example:
75
+
היא תרנדר `<LoginButton />`או`<LogoutButton />`כתלות ב-state הנוכחי שלה. בנוסף היא תרנדר `<Greeting />`מהדוגמה הקודמת:
76
76
77
77
```javascript{20-25,29,30}
78
78
class LoginControl extends React.Component {
@@ -116,13 +116,13 @@ ReactDOM.render(
116
116
);
117
117
```
118
118
119
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/QKzAgB?editors=0010)
119
+
[**נסו זאת ב-CodePen**](https://codepen.io/gaearon/pen/QKzAgB?editors=0010)
120
120
121
-
While declaring a variable and using an `if`statement is a fine way to conditionally render a component, sometimes you might want to use a shorter syntax. There are a few ways to inline conditions in JSX, explained below.
121
+
בעוד שהכרזה על משתנה ושימוש בהצהרה `if`היא דרך מצוינת להתנות רינדור קומפוננטה, לפעמים ייתכן שתרצו להשתמש בתחביר קצר יותר. ישנן מספר דרכים להטמיע תנאים ב-JSX, שמוסברות בהמשך.
122
122
123
-
### Inline If with Logical && Operator {#inline-if-with-logical--operator}
123
+
### הטמעת תנאי If עם אופרטור && לוגי {#inline-if-with-logical--operator}
124
124
125
-
You may [embed any expressions in JSX](/docs/introducing-jsx.html#embedding-expressions-in-jsx)by wrapping them in curly braces. This includes the JavaScript logical`&&`operator. It can be handy for conditionally including an element:
125
+
ייתכן שתרצו [להטמיע כל ביטוי ב-JSX](/docs/introducing-jsx.html#embedding-expressions-in-jsx)על ידי עטיפתם בסוגריים מסולסלים. זה כולל את האופרטור הלוגי`&&`של JavaScript. זה יכול להיות שימושי עבור הוספה מותנית של אלמנט:
126
126
127
127
```js{6-10}
128
128
function Mailbox(props) {
@@ -132,7 +132,7 @@ function Mailbox(props) {
132
132
<h1>Hello!</h1>
133
133
{unreadMessages.length > 0 &&
134
134
<h2>
135
-
You have {unreadMessages.length} unread messages.
135
+
יש לך {unreadMessages.length} הודעות שלא נקראו.
136
136
</h2>
137
137
}
138
138
</div>
@@ -146,30 +146,30 @@ ReactDOM.render(
146
146
);
147
147
```
148
148
149
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/ozJddz?editors=0010)
149
+
[**נסו זאת ב-CodePen**](https://codepen.io/gaearon/pen/ozJddz?editors=0010)
150
150
151
-
It works because in JavaScript, `true && expression`always evaluates to `expression`, and `false && expression`always evaluates to `false`.
151
+
זה עובד בגלל שב-JavaScript, `true && expression`תמיד שווה ערך ל-`expression`, ו-`false && expression`תמיד שווה ערך ל-`false`.
152
152
153
-
Therefore, if the condition is `true`, the element right after `&&`will appear in the output. If it is `false`, React will ignore and skip it.
153
+
לכן, אם התנאי הוא `true`, האלמנט מימין אחרי `&&`יופיע בפלט. אם הוא `false`, React תתעלם ממנו ותדלג עליו.
154
154
155
-
### Inline If-Else with Conditional Operator {#inline-if-else-with-conditional-operator}
155
+
### הטמעת If-Else עם אופרטור ההתנייה {#inline-if-else-with-conditional-operator}
156
156
157
-
Another method for conditionally rendering elements inline is to use the JavaScript conditional operator[`condition ? true : false`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator).
157
+
שיטה נוספת להטמעת רינדור אלמנטים מותנה היא להשתמש באופרטור ההתנייה של JavaScript [`condition ? true : false`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator).
158
158
159
-
In the example below, we use it to conditionally render a small block of text.
159
+
בדוגמה הבאה, אנו משתמשים בו כדי לרנדר באופן מותנה בלוק קטן של טקסט.
160
160
161
161
```javascript{5}
162
162
render() {
163
163
const isLoggedIn = this.state.isLoggedIn;
164
164
return (
165
165
<div>
166
-
The user is <b>{isLoggedIn ? 'currently' : 'not'}</b> logged in.
166
+
המשתמש <b>{isLoggedIn ? 'כרגע' : 'לא'}</b> מחובר.
167
167
</div>
168
168
);
169
169
}
170
170
```
171
171
172
-
It can also be used for larger expressions although it is less obvious what's going on:
172
+
זה יכול לשמש גם עבור ביטויים גדולים יותר למרות שכך פחות ברור מה קורה:
173
173
174
174
```js{5,7,9}
175
175
render() {
@@ -186,13 +186,13 @@ render() {
186
186
}
187
187
```
188
188
189
-
Just like in JavaScript, it is up to you to choose an appropriate style based on what you and your team consider more readable. Also remember that whenever conditions become too complex, it might be a good time to [extract a component](/docs/components-and-props.html#extracting-components).
189
+
בדיוק כמו ב-JavaScript, זה באחריותכם לבחור סגנון מתאים על סמך מה שאתם והצוות שלכם מחשיבים יותר קריא. כמו כן זכרו שבכל פעם שתנאים נהיים מורכבים מדי, זה יכול להיות זמן טוב [לחלץ קומפוננטה](/docs/components-and-props.html#extracting-components).
190
190
191
-
### Preventing Component from Rendering {#preventing-component-from-rendering}
191
+
### מניעת רינדור של קומפוננטה {#preventing-component-from-rendering}
192
192
193
-
In rare cases you might want a component to hide itself even though it was rendered by another component. To do this return`null`instead of its render output.
193
+
במקרים נדירים ייתכן שתרצו שקומפוננטה תסתיר את עצמה למרות שהיא רונדרה על ידי קומפוננטה אחרת. כדי לעשות זאת החזירו`null`במקום את הפלט שאותו היא אמורה לרנדר.
194
194
195
-
In the example below, the `<WarningBanner />`is rendered depending on the value of the prop called`warn`. If the value of the prop is`false`, then the component does not render:
195
+
בדוגמה הבאה, ה-`<WarningBanner />`מרונדר בהתאם לערך של ה-prop שנקרא`warn`. אם ערך ה-prop הוא`false`, אזי הרכיב אינו מתרנדר:
196
196
197
197
```javascript{2-4,29}
198
198
function WarningBanner(props) {
@@ -202,7 +202,7 @@ function WarningBanner(props) {
202
202
203
203
return (
204
204
<div className="warning">
205
-
Warning!
205
+
אזהרה!
206
206
</div>
207
207
);
208
208
}
@@ -225,7 +225,7 @@ class Page extends React.Component {
225
225
<div>
226
226
<WarningBanner warn={this.state.showWarning} />
227
227
<button onClick={this.handleToggleClick}>
228
-
{this.state.showWarning ? 'Hide' : 'Show'}
228
+
{this.state.showWarning ? 'הסתר' : 'הצג'}
229
229
</button>
230
230
</div>
231
231
);
@@ -238,6 +238,6 @@ ReactDOM.render(
238
238
);
239
239
```
240
240
241
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/Xjoqwm?editors=0010)
241
+
[**נסו זאת ב-CodePen**](https://codepen.io/gaearon/pen/Xjoqwm?editors=0010)
242
242
243
-
Returning`null`from a component's `render`method does not affect the firing of the component's lifecycle methods. For instance `componentDidUpdate`will still be called.
243
+
החזרת`null`ממתודת `render`של קומפוננטה לא משפיעה על הרצת מתודות מחזור החיים של קומפוננטה. למשל `componentDidUpdate`עדיין יקרא.
0 commit comments