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
HTML form elements work a little bit differently from other DOM elements in React, because form elements naturally keep some internal state. For example, this form in plain HTML accepts a single name:
12
+
אלמנטים של טפסיHTML עובדים מעט מאשר אלמנטים אחרים של ה-DOM ב-React, מכיוון שאלמנטים של טפסים באופן טבעי שומרים על state פנימי. למשל, הטופב הבא ב-HTML פשוט מקבל שם אחד:
13
13
14
14
```html
15
15
<form>
16
16
<label>
17
-
Name:
17
+
שם:
18
18
<inputtype="text"name="name" />
19
19
</label>
20
20
<inputtype="submit"value="Submit" />
21
21
</form>
22
22
```
23
23
24
-
This form has the default HTML form behavior of browsing to a new page when the user submits the form. If you want this behavior in React, it just works. But in most cases, it's convenient to have a JavaScript function that handles the submission of the form and has access to the data that the user entered into the form. The standard way to achieve this is with a technique called "controlled components".
24
+
לטופס זה יש את התנהגות ברירת המחדל של טפסי HTML של מעבר לדף חדש כאשר המשתמש שולח את הטופס. אם אתם רוצים את התנהגות זו ב-React, ככה זה עובד. אבל ברוב המקרים, זה נוח שיש פונקצית JavaScript המטפלת בשליחת הטופס ויש לה גישה לנתונים שהמשתמש הכניס לטופס. הדרך הסטנדרטית להשיג זאת היא באמצעות טכניקה הנקראת "קומפוננטות מבוקרות".
25
25
26
-
## Controlled Components {#controlled-components}
26
+
## קומפוננטות מבוקרות {#controlled-components}
27
27
28
-
In HTML, form elements such as`<input>`, `<textarea>`, and `<select>` typically maintain their own state and update it based on user input. In React, mutable state is typically kept in the state property of components, and only updated with[`setState()`](/docs/react-component.html#setstate).
28
+
ב-HTML, אלמנטים של טופס כגון`<input>`, `<textarea>`, ו-`<select>`שומרים בדרך כלל על מצבם ומעדכנים אותו על סמך קלט מהמשתמש. ב-React, `state` בר-שינוי נשמר בדרך כלל במאפייני ה-state של קומפוננטות, ומעודכן רק עם[`setState()`](/docs/react-component.html#setstate).
29
29
30
-
We can combine the two by making the React state be the "single source of truth". Then the React component that renders a form also controls what happens in that form on subsequent user input. An input form element whose value is controlled by React in this way is called a "controlled component".
30
+
אנחנו יכולים לשלב את השניים על ידי הפיכת ה-state של React להיות "single source of truth" (מקור אמת יחיד). לאחר מכן קומפוננטת ה-React שמרנדרת טופס גם שולטת במה שקורה באותו טופס על קלט המשתמש הבא. קלט מאלמנט טופס שערכו נשלט על ידי React בדרך זו נקרא "קומפוננטה מבוקרת".
31
31
32
-
For example, if we want to make the previous example log the name when it is submitted, we can write the form as a controlled component:
32
+
לדוגמה, אם אנחנו רוצים לשנות את הדוגמה הקודמת כך שתרשום ל-log את השם כאשר הטופס נשלח, אנחנו יכולים לרשום את הטופס כקומפונטה מבוקרת:
33
33
34
34
```javascript{4,10-12,24}
35
35
class NameForm extends React.Component {
@@ -46,54 +46,54 @@ class NameForm extends React.Component {
46
46
}
47
47
48
48
handleSubmit(event) {
49
-
alert('A name was submitted: ' + this.state.value);
[**Try it on CodePen**](https://codepen.io/gaearon/pen/VmmPgp?editors=0010)
67
+
[**נסו זאת ב-CodePen**](https://codepen.io/gaearon/pen/VmmPgp?editors=0010)
68
68
69
-
Since the`value`attribute is set on our form element, the displayed value will always be `this.state.value`, making the React state the source of truth. Since `handleChange`runs on every keystroke to update the React state, the displayed value will update as the user types.
69
+
מאחר שהתכונה`value`מוגדרת על אלמנט הטופס שלנו, הערך המוצג תמיד יהיה `this.state.value`, מה שהופך את ה-state של React ל-source of truth (מקור האמת). מכיוון ש-`handleChange`פועל על כל הקשה כדי לעדכן את ה-state של React, הערך המוצג יעודכן בזמן שהמשתמש מקליד.
70
70
71
-
With a controlled component, every state mutation will have an associated handler function. This makes it straightforward to modify or validate user input. For example, if we wanted to enforce that names are written with all uppercase letters, we could write`handleChange`as:
71
+
עם קומפוננטה מבוקרת, לכל שינוי של ה-state תהיה פונקציה מטפלת מקושרת אליו. זה הופך את זה לממש פשוט לשנות או לאמת קלט משתמש. לדוגמה, אם רצינו לאכוף את השמות כך שיהיו כתובים באותיות גדולות, נוכל לכתוב את`handleChange`כך:
In HTML, a`<textarea>`element defines its text by its children:
81
+
ב-HTML, אלמנט`<textarea>`מגדיר את הטקסט שלו על ידי הילדים שלו:
82
82
83
83
```html
84
84
<textarea>
85
-
Hello there, this is some text in a text area
85
+
היי, זה מעט טקסט ב-textarea
86
86
</textarea>
87
87
```
88
88
89
-
In React, a `<textarea>`uses a`value`attribute instead. This way, a form using a `<textarea>`can be written very similarly to a form that uses a single-line input:
89
+
ב-React, `<textarea>`משתמש במאפיין`value`במקום. בדרך זו, טופס המשתמש ב-`<textarea>`יכול להכתב באופן מאוד דומה לטופס המשתמש ב-input של שורה אחת:
90
90
91
91
```javascript{4-6,12-14,26}
92
92
class EssayForm extends React.Component {
93
93
constructor(props) {
94
94
super(props);
95
95
this.state = {
96
-
value: 'Please write an essay about your favorite DOM element.'
96
+
value: 'אנא כתבו מאמר אודות אלמנט ה-DOM האהוב עליכם.'
97
97
};
98
98
99
99
this.handleChange = this.handleChange.bind(this);
@@ -105,7 +105,7 @@ class EssayForm extends React.Component {
105
105
}
106
106
107
107
handleSubmit(event) {
108
-
alert('An essay was submitted: ' + this.state.value);
108
+
alert('מאמר נשלח: ' + this.state.value);
109
109
event.preventDefault();
110
110
}
111
111
@@ -116,29 +116,29 @@ class EssayForm extends React.Component {
Notice that `this.state.value`is initialized in the constructor, so that the text area starts off with some text in it.
126
+
שימו לב ש-`this.state.value`מאותחל בבנאי, כך שה-text area מאותחל עם קצת טקסט בתוכו.
127
127
128
-
## The select Tag {#the-select-tag}
128
+
## תגית select {#the-select-tag}
129
129
130
-
In HTML, `<select>`creates a drop-down list. For example, this HTML creates a drop-down list of flavors:
130
+
ב-HTML, `<select>`מייצר רשימה נפתחת. למשל, ה-HTML הבא מייצר רשימה נפתחת של טעמים:
131
131
132
132
```html
133
133
<select>
134
-
<optionvalue="grapefruit">Grapefruit</option>
135
-
<optionvalue="lime">Lime</option>
136
-
<optionselectedvalue="coconut">Coconut</option>
137
-
<optionvalue="mango">Mango</option>
134
+
<optionvalue="grapefruit">אשכולית</option>
135
+
<optionvalue="lime">ליים</option>
136
+
<optionselectedvalue="coconut">קוקוס</option>
137
+
<optionvalue="mango">מנגו</option>
138
138
</select>
139
139
```
140
140
141
-
Note that the Coconut option is initially selected, because of the `selected` attribute. React, instead of using this `selected` attribute, uses a`value`attribute on the root `select` tag. This is more convenient in a controlled component because you only need to update it in one place. For example:
141
+
שימו לב שהאפשרות קוקוס נבחרה תחילה, בגלל השימוש במאפיין`selected`. React, במקום להשתמש במאפיין `selected`, משתמשת במאפיין`value`של תגית השורש `select`. זה נוח יותר בקומפוננטה מבוקרת מכיוון שאתם צריכים לעדכן אותו רק במקום אחד. לדוגמה:
142
142
143
143
```javascript{4,10-12,24}
144
144
class FlavorForm extends React.Component {
@@ -155,7 +155,7 @@ class FlavorForm extends React.Component {
155
155
}
156
156
157
157
handleSubmit(event) {
158
-
alert('Your favorite flavor is: ' + this.state.value);
158
+
alert('הטעם האהוב עליך: ' + this.state.value);
159
159
event.preventDefault();
160
160
}
161
161
@@ -165,46 +165,48 @@ class FlavorForm extends React.Component {
[**Try it on CodePen**](https://codepen.io/gaearon/pen/JbbEzX?editors=0010)
181
+
[**נסו זאת ב-CodePen**](https://codepen.io/gaearon/pen/JbbEzX?editors=0010)
182
182
183
-
Overall, this makes it so that `<input type="text">`, `<textarea>`, and `<select>`all work very similarly - they all accept a `value`attribute that you can use to implement a controlled component.
183
+
באופן כללי, זה גורם לכך ש-`<input type="text">`, `<textarea>`, ו-`<select>`כולם עובדים באופן מאוד דומה - כולם מקבלים מאפיין `value`שבו ניתן להשתמש כדי לממש קומפוננטה מבוקרת.
184
184
185
-
> Note
185
+
> הערה
186
186
>
187
-
> You can pass an array into the `value`attribute, allowing you to select multiple options in a `select` tag:
187
+
> ניתן להעביר למאפיין `value`מערך, המאפשר לכם לבחור אפשרויות מרובות בתגית `select`:
188
188
>
189
189
>```js
190
190
><select multiple={true} value={['B', 'C']}>
191
191
>```
192
192
193
-
## The file input Tag {#the-file-input-tag}
193
+
## תגית ה-file input {#the-file-input-tag}
194
194
195
-
In HTML, an`<input type="file">`lets the user choose one or more files from their device storage to be uploaded to a server or manipulated by JavaScript via the [File API](https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications).
195
+
ב-HTML, תגית`<input type="file">`מאפשרת למשתמש לבחור קובץ אחד או יותר מזכרון המכשיר שלהם להעלאה לשרת או לביצוע מניפולציות על ידי JavaScript דרך ה-[File API](https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications).
196
196
197
197
```html
198
198
<input type="file"/>
199
199
```
200
200
201
-
Because its value is read-only, it is an **uncontrolled** component in React. It is discussed together with other uncontrolled components [later in the documentation](/docs/uncontrolled-components.html#the-file-input-tag).
When you need to handle multiple controlled `input` elements, you can add a `name` attribute to each element and let the handler function choose what to do based on the value of `event.target.name`.
203
+
מכיוון שהערך שלו הוא לקריאה בלבד, זוהי קומפוננטה **שאינה מבוקרת** ב-React. היא נדונה בהרחבה יחד עם קומפוננטות בלתי מבוקרות אחרות [בשלב מאוחר יותר בתיעוד](/docs/uncontrolled-components.html#the-file-input-tag).
206
204
207
-
For example:
205
+
## טיפול בקלטים מרובים {#handling-multiple-inputs}
206
+
207
+
כאשר אתם צריכים להתמודד עם מספר אלמנטי `input` מבוקרים, אתם יכולים להוסיף מאפיין `name` לכל אלמנט ולתת לפונקציה המטפלת לבחור מה לעשות על סמך הערך של `event.target.name`.
208
+
209
+
לדוגמה:
208
210
209
211
```javascript{15,18,28,37}
210
212
class Reservation extends React.Component {
@@ -232,7 +234,7 @@ class Reservation extends React.Component {
232
234
return (
233
235
<form>
234
236
<label>
235
-
Is going:
237
+
מגיעים:
236
238
<input
237
239
name="isGoing"
238
240
type="checkbox"
@@ -241,7 +243,7 @@ class Reservation extends React.Component {
241
243
</label>
242
244
<br />
243
245
<label>
244
-
Number of guests:
246
+
מספר אורחים:
245
247
<input
246
248
name="numberOfGuests"
247
249
type="number"
@@ -254,45 +256,45 @@ class Reservation extends React.Component {
254
256
}
255
257
```
256
258
257
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/wgedvV?editors=0010)
259
+
[**נסו זאת ב-CodePen**](https://codepen.io/gaearon/pen/wgedvV?editors=0010)
258
260
259
-
Note how we used the ES6 [computed property name](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names)syntax to update the state key corresponding to the given input name:
261
+
שימו לב כיצד אנו משתמשים בתחביר [שם מאפיין מחושב](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names)של ES6 כדי לעדכן את מפתח ה-state בהתאמה לשם הקלט שהתקבל:
260
262
261
263
```js{2}
262
264
this.setState({
263
265
[name]: value
264
266
});
265
267
```
266
268
267
-
It is equivalent to this ES5 code:
269
+
זה שווה ערך לקוד ה-ES5 הזה:
268
270
269
271
```js{2}
270
272
var partialState = {};
271
273
partialState[name] = value;
272
274
this.setState(partialState);
273
275
```
274
276
275
-
Also, since `setState()`automatically [merges a partial state into the current state](/docs/state-and-lifecycle.html#state-updates-are-merged), we only needed to call it with the changed parts.
277
+
בנוסף, מאחר ש-`setState()`[ממזגת state חלקי ל-state הנוכחי](/docs/state-and-lifecycle.html#state-updates-are-merged) אוטומטית, אנחנו רק צריכים לקרוא לה עם החלקים השתנו.
276
278
277
-
## Controlled Input Null Value {#controlled-input-null-value}
279
+
## ערך ריק בקלט מבוקר {#controlled-input-null-value}
278
280
279
-
Specifying the value prop on a [controlled component](/docs/forms.html#controlled-components)prevents the user from changing the input unless you desire so. If you've specified a `value`but the input is still editable, you may have accidentally set`value`to `undefined`or`null`.
281
+
ציון ערך ה-prop על [קומפוננטה מבוקרת](/docs/forms.html#controlled-components)מונעת מהמשתמש לשנות את הקלט אלא אם כן אתם חפצים בכך. אם ציינתם ערך `value`אבל ה-input עדיין ניתן לעריכה, יכול להיות שבטעות הגדרתם את`value`ל-`undefined`או`null`.
280
282
281
-
The following code demonstrates this. (The input is locked at first but becomes editable after a short delay.)
283
+
הקוד הבא מדגים זאת. (ה-input נעול בהתחלה, אך הופך לניתן לעריכה לאחר עיכוב קצר).
It can sometimes be tedious to use controlled components, because you need to write an event handler for every way your data can change and pipe all of the input state through a React component. This can become particularly annoying when you are converting a preexisting codebase to React, or integrating a React application with a non-React library. In these situations, you might want to check out [uncontrolled components](/docs/uncontrolled-components.html), an alternative technique for implementing input forms.
296
+
זה יכול לפעמים להיות מייגע להשתמש בקומפוננטות מבוקרות, כי אתם צריכים לכתוב מטפל אירוע עבור כל דרך בה הנתונים שלכם יכולים להשתנות ולשרשר את כל מצבי הקלט באמצעות קומפוננטת React. זה יכול להיות מעצבן במיוחד כאשר אתם ממירים קוד קוד קיים ל-React, או משלבים אפליקציית React עם ספרייה שאינה React. במצבים אלו, ייתכן שתרצו לבדוק על [קומפוננטות בלתי מבוקרות](/docs/uncontrolled-components.html), טכניקה חלופית למימוש טפסי קלט.
If you're looking for a complete solution including validation, keeping track of the visited fields, and handling form submission, [Formik](https://jaredpalmer.com/formik)is one of the popular choices. However, it is built on the same principles of controlled components and managing state — so don't neglect to learn them.
300
+
אם אתם מחפשים פתרון מלא הכולל אימות, מעקב אחר שדות שבוקרו, וטיפול בשליחת טופס, [Formik](https://jaredpalmer.com/formik)היא אחת האפשרויות הפופולריות. עם זאת, הוא בנוי על אותם עקרונות של קומפוננטות מבוקרות וניהול state — אז אל תזניחו לימוד שלהם.
0 commit comments