Skip to content

Commit f043662

Browse files
authored
Merge pull request #77 from rgrnwd/master
Translate Advanced (context, forwarding refs) pages
2 parents 90c9275 + 0b3aba3 commit f043662

15 files changed

+149
-147
lines changed

content/docs/context.md

Lines changed: 72 additions & 71 deletions
Large diffs are not rendered by default.

content/docs/forwarding-refs.md

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,77 @@
11
---
22
id: forwarding-refs
3-
title: Forwarding Refs
3+
title: העברת רפרנסים
44
permalink: docs/forwarding-refs.html
55
---
66

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) דרך קומפוננטה לאחד מקומפוננטות הילד שלה. בדרך כלל, זאת טכניקה שלא נחוצה לרוב הקומפוננטות באפליקציה, אבל יכולה להיות שימושית במצבים מסוימים, בעיקר בספריות קומפוננטות רב פעמיות. התרחישים הנפוצים ביותר מתוארים פה.
88

9-
## Forwarding refs to DOM components {#forwarding-refs-to-dom-components}
9+
## העברת רפרנסים לקומפוננטות ב-DOM {#forwarding-refs-to-dom-components}
1010

11-
Consider a `FancyButton` component that renders the native `button` DOM element:
11+
נקח לדוגמא קומפוננטת כפתור `FancyButton` שמרנדרת אלמנט כפתור פשוט בתוך ה-DOM:
1212
`embed:forwarding-refs/fancy-button-simple.js`
1313

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 אחת של השנייה יותר מדי.
1515

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 שלהן כדי לשנות פוקוס, בחירה או אנימציה.
1717

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+
**העברת רפרנסים היא פיצ׳ר שמאפשר לקומפוננטות לקחת הפניה (`רפרנס`) שהם קיבלו ולהעביר אותה הלאה לקומפוננטת ילד**
1919

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:
2121

2222
`embed:forwarding-refs/fancy-button-simple-ref.js`
2323

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 באופן ישיר.
2525

26-
Here is a step-by-step explanation of what happens in the above example:
26+
להלן הסבר מפורט של מה שקורה בדוגמא לעיל:
2727

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.
3333

34-
>Note
34+
>הערה
3535
>
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 שלהן.
3737
>
38-
>Ref forwarding is not limited to DOM components. You can forward refs to class component instances, too.
38+
> העברת רפרנסים לא מוגבלת רק לקומפוננטות DOM. ניתן להעביר רפרנסים גם למופע של קומפוננטות מחלקה.
3939
40-
## Note for component library maintainers {#note-for-component-library-maintainers}
40+
## הערה למתחזקי ספריות קומפוננטות {#note-for-component-library-maintainers}
4141

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` בספריית קומפוננטות, צריך להתייחס לשינוי כשינוי שובר ולשחרר גרסה ראשית חדשה של הספרייה.** הסיבה לכך היא שכעת לספרייה יש התנהגות נצפית שונה (כמו איזה רפרנסים מיושמים ואיזה סוגי אובייקטים מיוצאים), וזה יכול לשבור אפליקציות וספריות אחרות שהיו תלויות בהתנהגות הקודמת.
4343

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 עצמה.
4545

46-
## Forwarding refs in higher-order components {#forwarding-refs-in-higher-order-components}
46+
## העברת רפרנסים בקומפוננטות מסדר גבוה יותר {#forwarding-refs-in-higher-order-components}
4747

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:
4950
`embed:forwarding-refs/log-props-before.js`
5051

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" שלנו:
5253
`embed:forwarding-refs/fancy-button.js`
5354

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 מתייחס אליו בצורה קצת שונה. אם תוסיפו את הרפרנס לקומפוננטה מסדר גבוה יותר, הרפרנס יתייחס לקונטיינר הקומפוננטה החיצוני, ולא לקומפוננטה העטופה.
5556

56-
This means that refs intended for our `FancyButton` component will actually be attached to the `LogProps` component:
57+
זה אומר שרפרנסים שיועדו לקומפוננטת ה-`FancyButton` שלנו יהיו דווקא מקושרים לקומפוננטת ה- `LogProps`:
5758
`embed:forwarding-refs/fancy-button-ref.js`
5859

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. לדוגמא:
6061
`embed:forwarding-refs/log-props-after.js`
6162

62-
## Displaying a custom name in DevTools {#displaying-a-custom-name-in-devtools}
63+
## הצגת שם מותאים בכלי פיתוח {#displaying-a-custom-name-in-devtools}
6364

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 משתמשים בפונקציה הזאת כדי להחליט מה להציג עבור קומפוננטת העברת הרפרנסים.
6566

66-
For example, the following component will appear as "*ForwardRef*" in the DevTools:
67+
לדוגמא, הקומפוננטה הבאה תוצג כ: "*ForwardRef*" בכלי הפיתוח של React:
6768

6869
`embed:forwarding-refs/wrapped-component.js`
6970

70-
If you name the render function, DevTools will also include its name (e.g. "*ForwardRef(myFunction)*"):
71+
אם ניתן שם לפונקציית הרינדור, כלי הפיתוח יציגו גם אותו (לדוגמא "*ForwardRef(myFunction)*"):
7172

7273
`embed:forwarding-refs/wrapped-component-with-function-name.js`
7374

74-
You can even set the function's `displayName` property to include the component you're wrapping:
75+
אפשר לציין את ה-`displayName` של הפונקצייה בצורה שתכלול את הקומפוננטה העטופה:
7576

7677
`embed:forwarding-refs/customized-display-name.js`

examples/context/motivation-problem.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ class App extends React.Component {
66

77
function Toolbar(props) {
88
// highlight-range{1-4,7}
9-
// The Toolbar component must take an extra "theme" prop
10-
// and pass it to the ThemedButton. This can become painful
11-
// if every single button in the app needs to know the theme
12-
// because it would have to be passed through all components.
9+
// "ערכת נושא" prop-קומפוננטת התפריט צריכה לקבל את ה
10+
// זה יכול להפוך את הקוד ."ThemedButton" ולהעביר אותו לכפתור
11+
// למסורבל אם כל כפתור באפליקציה צריך לדעת על ערכת הנושא בצורה זו
12+
// כי נאלץ להעביר אותה דרך כל הקומפוננטות.
1313
return (
1414
<div>
1515
<ThemedButton theme={props.theme} />

examples/context/motivation-solution.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// highlight-range{1-4}
2-
// Context lets us pass a value deep into the component tree
3-
// without explicitly threading it through every component.
4-
// Create a context for the current theme (with "light" as the default).
2+
// קונטקס נותן לנו להעביר ערך עמוק לתוך עץ הקומפוננטות
3+
// בלי להעביר אותו באופן מפורש לכל קומפוננטה.
4+
// (צור קונטקס לערכת הנושא (עם ברירת מחדל ״בהירה״
55
const ThemeContext = React.createContext('light');
66

77
class App extends React.Component {
88
render() {
99
// highlight-range{1-3,5}
10-
// Use a Provider to pass the current theme to the tree below.
11-
// Any component can read it, no matter how deep it is.
12-
// In this example, we're passing "dark" as the current value.
10+
// כדי להעביר את ערכת הנושא לעץ הקומפוננטות Provider-נשתמש ב
11+
// כל קומפוננטה יכולה להשתמש בו, לא משנה באיזה עומק.
12+
// בדוגמא הבאה, נעביר ״כהה״ כערך לערכת הנושא.
1313
return (
1414
<ThemeContext.Provider value="dark">
1515
<Toolbar />
@@ -19,8 +19,8 @@ class App extends React.Component {
1919
}
2020

2121
// highlight-range{1,2}
22-
// A component in the middle doesn't have to
23-
// pass the theme down explicitly anymore.
22+
// קומפוננטת ביניים כבר לא צריכה להעביר
23+
// את ערכת הנושא.
2424
function Toolbar(props) {
2525
return (
2626
<div>
@@ -31,9 +31,9 @@ function Toolbar(props) {
3131

3232
class ThemedButton extends React.Component {
3333
// highlight-range{1-3,6}
34-
// Assign a contextType to read the current theme context.
35-
// React will find the closest theme Provider above and use its value.
36-
// In this example, the current theme is "dark".
34+
// contextType-נקרא את ערך ערכת הנושא מהקונטקס ב.
35+
// ימצא את ספק ערכת הנושא הקרוב ויקרא את ערך ערכת הנושא React
36+
// בדוגמא הזאת, הערך הוא ״כהה״.
3737
static contextType = ThemeContext;
3838
render() {
3939
return <Button theme={this.context} />;

examples/context/multiple-contexts.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// Theme context, default to light theme
1+
// קונטקסט ערכת הנושא עם ברירת מחדל בהירה
22
const ThemeContext = React.createContext('light');
33

4-
// Signed-in user context
4+
// קונטקסט המשתמש המאומת
55
const UserContext = React.createContext({
66
name: 'Guest',
77
});
@@ -10,7 +10,7 @@ class App extends React.Component {
1010
render() {
1111
const {signedInUser, theme} = this.props;
1212

13-
// App component that provides initial context values
13+
// קומפוננטה שמספקת ערכי ברירת מחדל לקונטקסט
1414
// highlight-range{2-3,5-6}
1515
return (
1616
<ThemeContext.Provider value={theme}>
@@ -31,7 +31,7 @@ function Layout() {
3131
);
3232
}
3333

34-
// A component may consume multiple contexts
34+
// קומפוננטה יכולה לצרוך יותר מקונטקסט אחד
3535
function Content() {
3636
// highlight-range{2-10}
3737
return (

examples/context/theme-detailed-app.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {ThemeContext, themes} from './theme-context';
22
import ThemedButton from './themed-button';
33

4-
// An intermediate component that uses the ThemedButton
4+
// ThemedButton-רכיב ביניים שמשתמש ב
55
function Toolbar(props) {
66
return (
77
<ThemedButton onClick={props.changeTheme}>
@@ -29,9 +29,9 @@ class App extends React.Component {
2929

3030
render() {
3131
//highlight-range{1-3}
32-
// The ThemedButton button inside the ThemeProvider
33-
// uses the theme from state while the one outside uses
34-
// the default dark theme
32+
// בתוך ספק ערכת הנושא ThemedButton-כפתור ה
33+
// והכפתור החיצוני ,state-משתמש בערכת הנושא מה
34+
// משתמש בברירת המחדל של ערכת הנושא הכהה
3535
//highlight-range{3-5,7}
3636
return (
3737
<Page>

examples/context/theme-detailed-theme-context.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ export const themes = {
1111

1212
// highlight-range{1-3}
1313
export const ThemeContext = React.createContext(
14-
themes.dark // default value
14+
themes.dark // ברירת מחדל
1515
);

examples/context/updating-nested-context-app.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class App extends React.Component {
1515
};
1616

1717
// highlight-range{1-2,5}
18-
// State also contains the updater function so it will
19-
// be passed down into the context provider
18+
// מכיל גם את פונקצית העדכון state-ה
19+
// שתועבר לספק הקונטקסט
2020
this.state = {
2121
theme: themes.light,
2222
toggleTheme: this.toggleTheme,
@@ -25,7 +25,7 @@ class App extends React.Component {
2525

2626
render() {
2727
// highlight-range{1,3}
28-
// The entire state is passed to the provider
28+
// בשלמותו מועבר לספק state-ה
2929
return (
3030
<ThemeContext.Provider value={this.state}>
3131
<Content />

examples/context/updating-nested-context-context.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Make sure the shape of the default value passed to
2-
// createContext matches the shape that the consumers expect!
1+
// createContext-שים לב שצורת הערך של ברירת המחדל שמועבר ל
2+
// זהה לצורת הערכים שהצרכנים מצפים לקבל!
33
// highlight-range{2-3}
44
export const ThemeContext = React.createContext({
55
theme: themes.dark,

examples/context/updating-nested-context-theme-toggler-button.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import {ThemeContext} from './theme-context';
22

33
function ThemeTogglerButton() {
44
// highlight-range{1-2,5}
5-
// The Theme Toggler Button receives not only the theme
6-
// but also a toggleTheme function from the context
5+
// מקבל לא רק את ערכת הנושא Theme Toggler-כפתור ה
6+
// מהקונטקסט כדי לעדכן את ערכה toggleTheme אלא גם את הפונקציה
77
return (
88
<ThemeContext.Consumer>
99
{({theme, toggleTheme}) => (

0 commit comments

Comments
 (0)