Skip to content

Commit eccac00

Browse files
author
pmartyniak
committed
Translate "Refs Forwarding" page
1 parent 2c42043 commit eccac00

File tree

6 files changed

+51
-45
lines changed

6 files changed

+51
-45
lines changed

content/docs/forwarding-refs.md

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,82 @@
11
---
22
id: forwarding-refs
3-
title: Forwarding Refs
3+
title: Przekazywanie referencji
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+
Przekazywanie referencji (ang. "Ref forwarding") to technika, w której [referencję](/docs/refs-and-the-dom.html)
8+
do komponentu "podajemy dalej" do jego dziecka. Dla większości komponentów w aplikacji nie jest to potrzebne.
9+
Może jednak okazać się przydatne w niektórych przypadkach. W szczególności w _wielokrotnie używanych_ komponentach bibliotecznych.
10+
Najczęstsze scenariusze opisujemy poniżej.
811

9-
## Forwarding refs to DOM components {#forwarding-refs-to-dom-components}
12+
## Przekazywanie referencji do komponentów DOM {#forwarding-refs-to-dom-components}
1013

11-
Consider a `FancyButton` component that renders the native `button` DOM element:
14+
Rozważmy `FancyButton` - komponent, który renderuje natywny element DOM - przycisk:
1215
`embed:forwarding-refs/fancy-button-simple.js`
1316

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.
17+
Komponenty reactowe ukrywają szczegóły swojej implementacji, w tym także wyrenderowany HTML.
18+
Inne komponenty używające `FancyButton` **z reguły nie potrzebują** [mieć dostępu do referencji](/docs/refs-and-the-dom.html) do wewnętrznego elementu `button`.
19+
Jest to korzystne, gdyż zapobiega sytuacji, w której komponenty są za bardzo uzależnione od struktury drzewa DOM innych komponentów.
1520

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.
21+
Taka enkapsulacja jest pożądana na poziomie aplikacji, w komponentach takich jak `FeedStory` czy `Comment`. Natomiast może się okazać to niewygodne w przypadku komponentów wielokrotnego użytku, będących "liśćmi" drzewa. Np. `FancyButton` albo `MyTextInput`. Takie komponenty często używane są w wielu miejscach aplikacji w podobny sposób, jak zwyczajne elementy DOM typu `button` i `input`. W wiązku z tym bezpośredni dostęp do ich DOM może okazać się konieczy, aby obsłużyć fokus, zaznaczenie, czy animacje.
1722

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.**
23+
**Przekazywanie referencji jest opcjonalną funkcjonalnością, która pozwala komponentom wziąć referencję do nich przekazaną i "podać dalej" do swojego dziecka.**
1924

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:
25+
W poniższym przykładzie `FancyButton` używa `React.forwardRef`, by przejąć przekazaną do niego referencję i podać ją dalej do elementu `button`, który renderuje:
2126

2227
`embed:forwarding-refs/fancy-button-simple-ref.js`
2328

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.
29+
Tym sposobem komponenty używające `FancyButton` mają referencję do znajdującego się w głębi elementu `button`. Mogą więc w razie potrzeby operować na komponencie tak, jakby operowały bezpośrednio na natywnym elemencie DOM.
2530

26-
Here is a step-by-step explanation of what happens in the above example:
31+
Oto krok po kroku - co wydarzyło się w przykładzie powyżej:
2732

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.
33+
1. Tworzymy [referencję Reactową](/docs/refs-and-the-dom.html) wołając `React.createRef` i przypisujemy ją do stałej `ref`.
34+
1. Przekazujemy `ref` do `<FancyButton ref={ref}>` przypisując ją do atrybutu JSX.
35+
1. React przekazuje `ref` do funkcji `(props, ref) => ...` jako drugi argument wewnątrz `forwardRef`.
36+
1. Podajemy argument `ref` dalej do `<button ref={ref}>` przypisując go do atrybutu JSX.
37+
1. Gdy referencja jest zamontowana, `ref.current` będzie wskazywać na element DOM `<button>`.
3338

34-
>Note
39+
>Uwaga
3540
>
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.
41+
>Drugi argument `ref` istnieje tylko, gdy definiujesz komponent przy pomocy wywołania `React.forwardRef`. Zwyczajna funkcja lub klasa nie dostanie argumentu `ref`, nawet jako typowej właściwości Reactowej ("props").
3742
>
38-
>Ref forwarding is not limited to DOM components. You can forward refs to class component instances, too.
43+
>Przekazywanie referencji nie jest ograniczone do elementów drzewa DOM. Możesz także przekazywać referencje do instancji komponentów klasowych.
3944
40-
## Note for component library maintainers {#note-for-component-library-maintainers}
45+
## Uwaga dla autorów bibliotek komonentów {#note-for-component-library-maintainers}
4146

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.
47+
**Jeśli zaczniesz używać `forwardRef` w swojej bibliotece komponentów, powinno być to trakotwane jako zmiana krytyczna (ang. "breaking change"). W efekcie bibliteka powinna być wydana w nowej "wersji głównej" (ang. "major version", "major release").** Jest tak, ponieważ najprawdopodobniej Twoja biblioteka zauważalnie zmieniła zachowanie (np. inaczej przypinając referencje i eksportując inne typy). Może to popsuć działanie aplikacji, które są zależne od dawnego zachowania.
4348

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.
49+
Stosowanie `React.forwardRef` warunkowo, gdy ono istnieje, także nie jest zalecane z tego samego powodu: zmienia to zachowanie biblioteki i może zepsuć działanie aplikacji użytkowników, gdy zmienią wersję Reacta.
4550

46-
## Forwarding refs in higher-order components {#forwarding-refs-in-higher-order-components}
51+
## Przekazywanie referencji w Komponentach Wyższego Rzędu {#forwarding-refs-in-higher-order-components}
52+
53+
Omawiana technika może okazać się wyjątkowo przydatna w [Komponentach Wyższego Rzędu](/docs/higher-order-components.html) (KWR; ang. "Higher Order Components" lub "HOC"). Zacznijmy od przykładu KWR, który wypisuje w konsoli wszystkie właściwości komponentu:
4754

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

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:
57+
KWR `logProps` przekazuje wszystkie atrybuty do komponentu, który opakowuje, więc wyrenderowany wynik będzie taki sam. Na przykład możemy użyć tego KWRa do logowania atrybutów, które zostaną przekazane do naszego komponentu `FancyButton`:
5258
`embed:forwarding-refs/fancy-button.js`
5359

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.
60+
Powyższe rozwiązanie ma jeden minus: referencje nie zostaną przekazane do komponentu. Dzieje się tak, ponieważ `ref` nie jest atrybutem. Tak jak `key`, jest on obsługiwany prez Reacta inaczej. Referencja będzie w tym wypadku odnosiła się do najbardziej zewnętrznego kontenera, a nie do owiniętego komponentu.
5561

56-
This means that refs intended for our `FancyButton` component will actually be attached to the `LogProps` component:
62+
Oznacza to, ze referencje przenzaczone dla naszego komponentu `FancyButton` będą w praktyce przyczepione do komponentu `LogProps`.
5763
`embed:forwarding-refs/fancy-button-ref.js`
5864

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:
65+
Na szczęście możemy wprost przekazać referencję do wewnętrznego komponentu `FancyButton` używając API `React.forwardRef`. `React.forwardRef` przyjmuje funkcję renderującą, która otrzymuje parametry `props` oraz `ref`, a zwraca element Reactowy. Na przykład:
6066
`embed:forwarding-refs/log-props-after.js`
6167

62-
## Displaying a custom name in DevTools {#displaying-a-custom-name-in-devtools}
68+
## Wyświetlanie własnej nazwy w narzędziach deweloperskich {#displaying-a-custom-name-in-devtools}
6369

64-
`React.forwardRef` accepts a render function. React DevTools uses this function to determine what to display for the ref forwarding component.
70+
`React.forwardRef` przyjmuje funkcję renderującą. Narzędzia deweloperskie Reacta ("React DevTools") używają tej funkcji do określenia, jak pokazać komonent, który przekazuje referencję.
6571

66-
For example, the following component will appear as "*ForwardRef*" in the DevTools:
72+
Przykładowo, następujący komponent w narzędziach deweloperskich wyświetli się jako "*ForwardRef*":
6773

6874
`embed:forwarding-refs/wrapped-component.js`
6975

70-
If you name the render function, DevTools will also include its name (e.g. "*ForwardRef(myFunction)*"):
76+
Jeśli nazwiesz funkcję renderującą, narzędzia deweloperskie uwzględnią tę nazwę (np. "*ForwardRef(myFunction)*"):
7177

7278
`embed:forwarding-refs/wrapped-component-with-function-name.js`
7379

74-
You can even set the function's `displayName` property to include the component you're wrapping:
80+
Możesz nawet ustawić właściwość `displayName` funkcji tak, aby uwzględniała nazwę opakowanego komponentu:
7581

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

examples/forwarding-refs/customized-display-name.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ function logProps(Component) {
77
return <LogProps {...props} forwardedRef={ref} />;
88
}
99

10-
// Give this component a more helpful display name in DevTools.
11-
// e.g. "ForwardRef(logProps(MyComponent))"
10+
// Nadajmy temu komponentowi nazwę, która będzie bardziej czytelna w narzędziach deweloperskich.
11+
// np. "ForwardRef(logProps(MyComponent))"
1212
// highlight-range{1-2}
1313
const name = Component.displayName || Component.name;
1414
forwardRef.displayName = `logProps(${name})`;

examples/forwarding-refs/fancy-button-ref.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import FancyButton from './FancyButton';
33
// highlight-next-line
44
const ref = React.createRef();
55

6-
// The FancyButton component we imported is the LogProps HOC.
7-
// Even though the rendered output will be the same,
8-
// Our ref will point to LogProps instead of the inner FancyButton component!
9-
// This means we can't call e.g. ref.current.focus()
6+
// Komponent FancyButton, który zaimportowaliśmy jest tak naprawdę KWRem LogProps.
7+
// Mimo że wyświetlony rezultat będzie taki sam,
8+
// nasza referencja będzie wskazywała na LogProps zamiast na komponent FancyButton!
9+
// Oznacza to, że nie możemy wywołać np. metody ref.current.focus()
1010
// highlight-range{4}
1111
<FancyButton
1212
label="Click Me"

examples/forwarding-refs/fancy-button-simple-ref.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ const FancyButton = React.forwardRef((props, ref) => (
55
</button>
66
));
77

8-
// You can now get a ref directly to the DOM button:
8+
// Możesz teraz otrzymać bezpośrednią referencję do elementu „button:
99
const ref = React.createRef();
1010
<FancyButton ref={ref}>Click me!</FancyButton>;

examples/forwarding-refs/fancy-button.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class FancyButton extends React.Component {
66
// ...
77
}
88

9-
// Rather than exporting FancyButton, we export LogProps.
10-
// It will render a FancyButton though.
9+
// Zamiast FancyButton, exportujemy LogProps.
10+
// Jednak wyrenderowany zostanie FancyButton.
1111
// highlight-next-line
1212
export default logProps(FancyButton);

examples/forwarding-refs/log-props-after.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ function logProps(Component) {
99
// highlight-next-line
1010
const {forwardedRef, ...rest} = this.props;
1111

12-
// Assign the custom prop "forwardedRef" as a ref
12+
// 2. Przypiszmy nasz atrybut "forwardedRef" jako referencję
1313
// highlight-next-line
1414
return <Component ref={forwardedRef} {...rest} />;
1515
}
1616
}
1717

18-
// Note the second param "ref" provided by React.forwardRef.
19-
// We can pass it along to LogProps as a regular prop, e.g. "forwardedRef"
20-
// And it can then be attached to the Component.
18+
// 1. Zwróć uwagę na drugi parametr "ref" dostarczony przez React.forwardRef.
19+
// Możemy go przekazać dalej do LogProps jako zwyczajny atrybut, np. "forwardedRef".
20+
// Następnie może on być przypisany do komponentu wewnątrz.
2121
// highlight-range{1-3}
2222
return React.forwardRef((props, ref) => {
2323
return <LogProps {...props} forwardedRef={ref} />;

0 commit comments

Comments
 (0)