|
1 | 1 | ---
|
2 | 2 | id: forwarding-refs
|
3 |
| -title: Forwarding Refs |
| 3 | +title: העברת רפרנסים |
4 | 4 | permalink: docs/forwarding-refs.html
|
5 | 5 | ---
|
6 | 6 |
|
7 |
| -Ref forwarding is a technique for automatically passing a [ref](/docs/refs-and-the-dom.html) through a component to one of its children. This is typically not necessary for most components in the application. However, it can be useful for some kinds of components, especially in reusable component libraries. The most common scenarios are described below. |
| 7 | +העברת רפרנסים היא טכניקה להעברה אוטומטית של [רפרנס](/docs/refs-and-the-dom.html) דרך קומפוננטה לאחד מקומפוננטות הילד שלה. בדרך כלל, זאת טכניקה שלא נחוצה לרוב הקומפוננטות באפליקציה, אבל יכולה להיות שימושית במצבים מסוימים, בעיקר בספריות קומפוננטות רב פעמיות. התרחישים הנפוצים ביותר מתוארים פה. |
8 | 8 |
|
9 |
| -## Forwarding refs to DOM components {#forwarding-refs-to-dom-components} |
| 9 | +## העברת רפרנסים לקומפוננטות ב-DOM {#forwarding-refs-to-dom-components} |
10 | 10 |
|
11 |
| -Consider a `FancyButton` component that renders the native `button` DOM element: |
| 11 | +נקח לדוגמא קומפוננטת כפתור `FancyButton` שמרנדרת אלמנט כפתור פשוט בתוך ה-DOM: |
12 | 12 | `embed:forwarding-refs/fancy-button-simple.js`
|
13 | 13 |
|
14 |
| -React components hide their implementation details, including their rendered output. Other components using `FancyButton` **usually will not need to** [obtain a ref](/docs/refs-and-the-dom.html) to the inner `button` DOM element. This is good because it prevents components from relying on each other's DOM structure too much. |
| 14 | +קומפוננטות React מחביאות את פרטי הישום שלהן, כולל את הפלט המרונדר שלהן. קומפוננטות אחרות שמשתמשות ב-`FancyButton` **בדרך כלל לא יצטרכו** [להשיג רפרנס](/docs/refs-and-the-dom.html) לאלמנט הכפתור של ה-DOM שהיא מיישמת. זה דבר טוב, כי זה מונע מקומפוננטות לסמוך על מבנה ה-DOM אחת של השנייה יותר מדי. |
15 | 15 |
|
16 |
| -Although such encapsulation is desirable for application-level components like `FeedStory` or `Comment`, it can be inconvenient for highly reusable "leaf" components like `FancyButton` or `MyTextInput`. These components tend to be used throughout the application in a similar manner as a regular DOM `button` and `input`, and accessing their DOM nodes may be unavoidable for managing focus, selection, or animations. |
| 16 | +למרות שכימוס כזה רצויה בקומפוננטות ברמת האפליקציה כמו `FeedStory` או `Comment`, היא יכולה להפוך לפחות נוחה בזמן שימוש בקומפוננטות ״עלה״ שנועדות לשימוש חוזר כמו `FancyButton` או `MyTextInput`. השימוש בקומפוננטות האלה בדרך כלל חוזר על עצמו פעמים רבות באפליקציה, בדומה לשימוש ב- `button` ו- `input`, ולפעמים אין ברירה אלא לגשת ישירות לאלמנטי ה-DOM שלהן כדי לשנות פוקוס, בחירה או אנימציה. |
17 | 17 |
|
18 |
| -**Ref forwarding is an opt-in feature that lets some components take a `ref` they receive, and pass it further down (in other words, "forward" it) to a child.** |
| 18 | +**העברת רפרנסים היא פיצ׳ר שמאפשר לקומפוננטות לקחת הפניה (`רפרנס`) שהם קיבלו ולהעביר אותה הלאה לקומפוננטת ילד** |
19 | 19 |
|
20 |
| -In the example below, `FancyButton` uses `React.forwardRef` to obtain the `ref` passed to it, and then forward it to the DOM `button` that it renders: |
| 20 | +בדוגמא הנ״ל, הכפתור `FancyButton` משתמש ב-`React.forwardRef` כדי לקבל את הרפרנס שהועבר אליו, ואז מעביר אותו לאלמנט הכפתור שהוא מרנדר ב-DOM: |
21 | 21 |
|
22 | 22 | `embed:forwarding-refs/fancy-button-simple-ref.js`
|
23 | 23 |
|
24 |
| -This way, components using `FancyButton` can get a ref to the underlying `button` DOM node and access it if necessary—just like if they used a DOM `button` directly. |
| 24 | +בצורה זו, קומפוננטות שמשתמשות ב- `FancyButton` יכולות לקבל רפרנס לצומת הכפתור עצמה ולהשתמש בה בעת הצורך - כאילו הן השתמשו בכפתור ב-DOM באופן ישיר. |
25 | 25 |
|
26 |
| -Here is a step-by-step explanation of what happens in the above example: |
| 26 | +להלן הסבר מפורט של מה שקורה בדוגמא לעיל: |
27 | 27 |
|
28 |
| -1. We create a [React ref](/docs/refs-and-the-dom.html) by calling `React.createRef` and assign it to a `ref` variable. |
29 |
| -1. We pass our `ref` down to `<FancyButton ref={ref}>` by specifying it as a JSX attribute. |
30 |
| -1. React passes the `ref` to the `(props, ref) => ...` function inside `forwardRef` as a second argument. |
31 |
| -1. We forward this `ref` argument down to `<button ref={ref}>` by specifying it as a JSX attribute. |
32 |
| -1. When the ref is attached, `ref.current` will point to the `<button>` DOM node. |
| 28 | +1. אנחנו יוצרים [רפרנס של React](/docs/refs-and-the-dom.html) ע״י שימוש ב- `React.createRef` ושמים אותו במשתנה ה-`ref`. |
| 29 | +1. אנחנו מעבירים את הרפרנס למטה ל- FancyButton בעזרת השימוש במאפיין ה-JSX: `<FancyButton ref={ref}>` |
| 30 | +1. React מעבירה את הרפרנס לפונקציית ה- `(props, ref) => ...` כפרמטר השני בתוך `forwardRef`. |
| 31 | +1. אנחנו מעבירים את ארגומנט הרפרנס למטה לכפתור על ידי שימוש במאפיין ה-JSX: `<button ref={ref}>` |
| 32 | +1. כשהרפרנס מחובר, `ref.current` יצביע לצומת הכפתור ב-DOM. |
33 | 33 |
|
34 |
| ->Note |
| 34 | +>הערה |
35 | 35 | >
|
36 |
| ->The second `ref` argument only exists when you define a component with `React.forwardRef` call. Regular function or class components don't receive the `ref` argument, and ref is not available in props either. |
| 36 | +> ארגומנט הרפרנס השני קיים רק כשמגדירים קומפוננטה עם קריאה ל- `React.forwardRef`. פונקציות רגילות או קומפוננטות מחלקה לא מקבלות את ארגומנט ה-`ref`, והוא גם לא זמין ב-props שלהן. |
37 | 37 | >
|
38 |
| ->Ref forwarding is not limited to DOM components. You can forward refs to class component instances, too. |
| 38 | +> העברת רפרנסים לא מוגבלת רק לקומפוננטות DOM. ניתן להעביר רפרנסים גם למופע של קומפוננטות מחלקה. |
39 | 39 |
|
40 |
| -## Note for component library maintainers {#note-for-component-library-maintainers} |
| 40 | +## הערה למתחזקי ספריות קומפוננטות {#note-for-component-library-maintainers} |
41 | 41 |
|
42 |
| -**When you start using `forwardRef` in a component library, you should treat it as a breaking change and release a new major version of your library.** This is because your library likely has an observably different behavior (such as what refs get assigned to, and what types are exported), and this can break apps and other libraries that depend on the old behavior. |
| 42 | +**כשמתחילים להשתמש ב- `forwardRef` בספריית קומפוננטות, צריך להתייחס לשינוי כשינוי שובר ולשחרר גרסה ראשית חדשה של הספרייה.** הסיבה לכך היא שכעת לספרייה יש התנהגות נצפית שונה (כמו איזה רפרנסים מיושמים ואיזה סוגי אובייקטים מיוצאים), וזה יכול לשבור אפליקציות וספריות אחרות שהיו תלויות בהתנהגות הקודמת. |
43 | 43 |
|
44 |
| -Conditionally applying `React.forwardRef` when it exists is also not recommended for the same reasons: it changes how your library behaves and can break your users' apps when they upgrade React itself. |
| 44 | +השימוש המותנה ב- `React.forwardRef` כשהוא קיים גם כן לא מומלץ מאותה הסיבה: הוא משנה את התנהגות הספרייה ויכול לשבור את האפליקציות שמשתמשות בה כשהן משדגרות את גרסת ה-React עצמה. |
45 | 45 |
|
46 |
| -## Forwarding refs in higher-order components {#forwarding-refs-in-higher-order-components} |
| 46 | +## העברת רפרנסים בקומפוננטות מסדר גבוה יותר {#forwarding-refs-in-higher-order-components} |
47 | 47 |
|
48 |
| -This technique can also be particularly useful with [higher-order components](/docs/higher-order-components.html) (also known as HOCs). Let's start with an example HOC that logs component props to the console: |
| 48 | +הטכניקה הזאת יכולה להיות שימושית ביותר בשימוש עם [קומפוננטות מסדר גבוה יותר](/docs/higher-order-components.html) (שידועות גם כ-HOCs). |
| 49 | +נתחיל עם דוגמא של HOC שמתעדת props של קומפוננטות ל-console: |
49 | 50 | `embed:forwarding-refs/log-props-before.js`
|
50 | 51 |
|
51 |
| -The "logProps" HOC passes all `props` through to the component it wraps, so the rendered output will be the same. For example, we can use this HOC to log all props that get passed to our "fancy button" component: |
| 52 | +ה-HOC "logProps" מעביר את כל ה-`props` דרכו לקומפוננטה שהוא עוטף, ולכן הפלט יהיה זהה. לדוגמא, אפשר להשתמש ב-HOC הזה כדי לתעד את כל ה-props שמועברים לקומפוננטת כפתור ה-"fancy button" שלנו: |
52 | 53 | `embed:forwarding-refs/fancy-button.js`
|
53 | 54 |
|
54 |
| -There is one caveat to the above example: refs will not get passed through. That's because `ref` is not a prop. Like `key`, it's handled differently by React. If you add a ref to a HOC, the ref will refer to the outermost container component, not the wrapped component. |
| 55 | +יש הסתייגות אחת לדוגמא שהצגנו: רפרנסים לא יועברו הלאה. הסיבה היא ש-`ref` הוא לא prop. כמו `key`, React מתייחס אליו בצורה קצת שונה. אם תוסיפו את הרפרנס לקומפוננטה מסדר גבוה יותר, הרפרנס יתייחס לקונטיינר הקומפוננטה החיצוני, ולא לקומפוננטה העטופה. |
55 | 56 |
|
56 |
| -This means that refs intended for our `FancyButton` component will actually be attached to the `LogProps` component: |
| 57 | +זה אומר שרפרנסים שיועדו לקומפוננטת ה-`FancyButton` שלנו יהיו דווקא מקושרים לקומפוננטת ה- `LogProps`: |
57 | 58 | `embed:forwarding-refs/fancy-button-ref.js`
|
58 | 59 |
|
59 |
| -Fortunately, we can explicitly forward refs to the inner `FancyButton` component using the `React.forwardRef` API. `React.forwardRef` accepts a render function that receives `props` and `ref` parameters and returns a React node. For example: |
| 60 | +למזלנו, אפשר להעביר את הרפרנס בצורה מפורשת לקומפוננטה הפנימית `FancyButton` בעזרת הממשק `React.forwardRef`. `React.forwardRef` מקבל פונקציית רינדור עם פרמטרים `props` ו- `ref` ומחזיר צומת React. לדוגמא: |
60 | 61 | `embed:forwarding-refs/log-props-after.js`
|
61 | 62 |
|
62 |
| -## Displaying a custom name in DevTools {#displaying-a-custom-name-in-devtools} |
| 63 | +## הצגת שם מותאים בכלי פיתוח {#displaying-a-custom-name-in-devtools} |
63 | 64 |
|
64 |
| -`React.forwardRef` accepts a render function. React DevTools uses this function to determine what to display for the ref forwarding component. |
| 65 | +`React.forwardRef` מקבל פונקציית רינדור. כלי הפיתוח של React משתמשים בפונקציה הזאת כדי להחליט מה להציג עבור קומפוננטת העברת הרפרנסים. |
65 | 66 |
|
66 |
| -For example, the following component will appear as "*ForwardRef*" in the DevTools: |
| 67 | +לדוגמא, הקומפוננטה הבאה תוצג כ: "*ForwardRef*" בכלי הפיתוח של React: |
67 | 68 |
|
68 | 69 | `embed:forwarding-refs/wrapped-component.js`
|
69 | 70 |
|
70 |
| -If you name the render function, DevTools will also include its name (e.g. "*ForwardRef(myFunction)*"): |
| 71 | +אם ניתן שם לפונקציית הרינדור, כלי הפיתוח יציגו גם אותו (לדוגמא "*ForwardRef(myFunction)*"): |
71 | 72 |
|
72 | 73 | `embed:forwarding-refs/wrapped-component-with-function-name.js`
|
73 | 74 |
|
74 |
| -You can even set the function's `displayName` property to include the component you're wrapping: |
| 75 | +אפשר לציין את ה-`displayName` של הפונקצייה בצורה שתכלול את הקומפוננטה העטופה: |
75 | 76 |
|
76 | 77 | `embed:forwarding-refs/customized-display-name.js`
|
0 commit comments