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/render-props.md
+59-59Lines changed: 59 additions & 59 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,28 +1,28 @@
1
1
---
2
2
id: render-props
3
-
title: Render Props
3
+
title: Właściwość renderująca
4
4
permalink: docs/render-props.html
5
5
---
6
6
7
-
The term ["render prop"](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce)refers to a technique for sharing code between React components using a prop whose value is a function.
7
+
Określenie ["właściwość renderująca"](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce)odnosi się do techniki współdzielenia kodu pomiędzy komponentami reactowymi, przy użyciu właściwości, której wartością jest funkcja.
8
8
9
-
A component with a render prop takes a function that returns a React element and calls it instead of implementing its own render logic.
9
+
Komponent z właściwością renderującą przyjmuje funkcję, zwracającą element reactowy, a następnie wywołuje ją, nie implementując jednocześnie logiki renderowania tego komponentu.
10
10
11
11
```jsx
12
12
<DataProvider render={data=> (
13
-
<h1>Hello {data.target}</h1>
13
+
<h1>Cześć, {data.target}</h1>
14
14
)}/>
15
15
```
16
16
17
-
Libraries that use render props include[React Router](https://reacttraining.com/react-router/web/api/Route/render-func)and[Downshift](https://github.com/paypal/downshift).
17
+
Biblioteki używające właściwości renderujących to m.in.[React Router](https://reacttraining.com/react-router/web/api/Route/render-func)i[Downshift](https://github.com/paypal/downshift).
18
18
19
-
In this document, we’ll discuss why render props are useful, and how to write your own.
19
+
W tym dokumencie przedyskutujemy przydatność właściwości renderujących i dowiemy się, jak napisać je samodzielnie.
20
20
21
-
## Use Render Props for Cross-Cutting Concerns {#use-render-props-for-cross-cutting-concerns}
21
+
## Użycie właściwości renderujących przy zagadnieniach przekrojowych (ang. *cross-cutting concerns*) {#use-render-props-for-cross-cutting-concerns}
22
22
23
-
Components are the primary unit of code reuse in React, but it's not always obvious how to share the state or behavior that one component encapsulates to other components that need that same state.
23
+
Komponenty są podstawowym sposobem na wielokrotne używanie funkcjonalności w Reakcie. Nie zawsze jednak jest oczywiste, jak należy współdzielić stan czy zachowanie zawarte w jednym komponencie, tak aby miały do nich dostęp inne komponenty.
24
24
25
-
For example, the following component tracks the mouse position in a web app:
25
+
Za przykład niech posłuży komponent śledzący pozycję kursora w aplikacji internetowej:
26
26
27
27
```js
28
28
classMouseTrackerextendsReact.Component {
@@ -42,22 +42,22 @@ class MouseTracker extends React.Component {
<p>The current mouse position is ({this.state.x}, {this.state.y})</p>
45
+
<h1>Porusz myszką!</h1>
46
+
<p>Aktualna pozycja kursora to ({this.state.x}, {this.state.y})</p>
47
47
</div>
48
48
);
49
49
}
50
50
}
51
51
```
52
52
53
-
As the cursor moves around the screen, the component displays its (x, y) coordinates in a`<p>`.
53
+
Podczas gdy kursor przemieszcza się po ekranie, komponent wyświetla jego koordynaty (x, y) wewnątrz znacznika`<p>`.
54
54
55
-
Now the question is: How can we reuse this behavior in another component? In other words, if another component needs to know about the cursor position, can we encapsulate that behavior so that we can easily share it with that component?
55
+
Pytanie brzmi: jak użyć ponownie tego zachowania w innym komponencie? Innymi słowy, jeżeli inny komponent potrzebuje współrzędnych pozycji kursora, czy możemy zaimplementować to zachowanie tak, abyśmy mogli łatwo współdzielić je z tym komponentem?
56
56
57
-
Since components are the basic unit of code reuse in React, let's try refactoring the code a bit to use a `<Mouse>`component that encapsulates the behavior we need to reuse elsewhere.
57
+
Ponieważ komponenty są podstawowym sposobem na wielokrotne używanie funkcjonalności w Reakcie, spróbujmy przekształcić trochę powyższy kod tak, aby wykorzystać komponent `<Mouse>`zawierający w sobie zachowanie wymagane przez inny komponent.
58
58
59
59
```js
60
-
//The <Mouse> component encapsulates the behavior we need...
60
+
//Komponent <Mouse> posiada funkcjonalność, której potrzebujemy...
61
61
classMouseextendsReact.Component {
62
62
constructor(props) {
63
63
super(props);
@@ -76,7 +76,7 @@ class Mouse extends React.Component {
{/* ...but how do we render something other than a <p>? */}
79
+
{/* ...ale jak wyrenderować coś innego niż <p>? */}
80
80
<p>The current mouse position is ({this.state.x}, {this.state.y})</p>
81
81
</div>
82
82
);
@@ -87,19 +87,19 @@ class MouseTracker extends React.Component {
87
87
render() {
88
88
return (
89
89
<div>
90
-
<h1>Move the mouse around!</h1>
90
+
<h1>Porusz myszką!</h1>
91
91
<Mouse />
92
92
</div>
93
93
);
94
94
}
95
95
}
96
96
```
97
97
98
-
Now the`<Mouse>`component encapsulates all behavior associated with listening for`mousemove`events and storing the (x, y) position of the cursor, but it's not yet truly reusable.
98
+
Teraz komponent`<Mouse>`implementuje wszystkie zachowania związane z nasłuchiwaniem na zdarzenie`mousemove`i przechowuje pozycję kursora (x, y), jednak nie jest jeszcze w pełni reużywalny.
99
99
100
-
For example, let's say we have a `<Cat>` component that renders the image of a cat chasing the mouse around the screen. We might use a `<Cat mouse={{ x, y }}>` prop to tell the component the coordinates of the mouse so it knows where to position the image on the screen.
100
+
Dla przykładu, powiedzmy że mamy komponent `<Cat>`, który renderuje obrazek kota goniącego kursor na ekranie. Moglibyśmy użyć właściwości `<Cat mouse={{ x, y }}>`, aby przekazać komponentowi koordynaty kursora, tak aby wiedział, gdzie umieścić obrazek kota.
101
101
102
-
As a first pass, you might try rendering the `<Cat>`*inside `<Mouse>`'s`render`method*, like this:
102
+
Początkowo możesz chcieć wyrenderować komponent `<Cat>`*wewnątrz metody`render`komponentu `<Mouse>`*, na przykład w ten sposób:
103
103
104
104
```js
105
105
classCatextendsReact.Component {
@@ -130,10 +130,10 @@ class MouseWithCat extends React.Component {
dla każdego przypadku użycia, dlatego też <MouseWithCat>
136
+
nie jest jeszcze w pełni używalny wielokrotnie.
137
137
*/}
138
138
<Cat mouse={this.state} />
139
139
</div>
@@ -145,17 +145,17 @@ class MouseTracker extends React.Component {
145
145
render() {
146
146
return (
147
147
<div>
148
-
<h1>Move the mouse around!</h1>
148
+
<h1>Porusz myszką!</h1>
149
149
<MouseWithCat />
150
150
</div>
151
151
);
152
152
}
153
153
}
154
154
```
155
155
156
-
This approach will work for our specific use case, but we haven't achieved the objective of truly encapsulating the behavior in a reusable way. Now, every time we want the mouse position for a different use case, we have to create a new component (i.e. essentially another`<MouseWithCat>`) that renders something specifically for that use case.
156
+
To podejście zadziałałoby w naszym specyficznym przypadku, ale nie osiągneliśmy prawdziwego celu, jakim jest hermetyzacja zachowania w taki sposób, aby można było je wykorzystywać wielokrotnie. W takim przypadku, za każdym razem gdy potrzebowalibyśmy inaczej wyświetlić coś na podstawie pozycji kursora, musielibyśmy stworzyć nowy komponent (innymi słowy, kolejny`<MouseWithCat>`), który wyrenderuje coś w odpowiedni sposób.
157
157
158
-
Here's where the render prop comes in: Instead of hard-coding a`<Cat>`inside a`<Mouse>` component, and effectively changing its rendered output, we can provide `<Mouse>`with a function prop that it uses to dynamically determine what to render–a render prop.
158
+
I tutaj do akcji wkracza właściwość renderująca. Zamiast na stałe osadzać`<Cat>`wewnątrz komponentu`<Mouse>`, jednocześnie zmieniając wyrenderowany przez niego wynik, możemy dostarczyć do komponentu `<Mouse>`właściwość będącą funkcją. Funkcja ta w sposób dynamiczny będzie określać, co powinno zostać wyrenderowane.
159
159
160
160
```js
161
161
classCatextendsReact.Component {
@@ -186,8 +186,8 @@ class Mouse extends React.Component {
Instead of providing a static representation of what <Mouse> renders,
190
-
use the `render` prop to dynamically determine what to render.
189
+
Zamiast używać statycznej reprezentacji tego co renderuje <Mouse>,
190
+
użyj właściwości `render`, aby dynamicznie określić, co należy wyrenderować.
191
191
*/}
192
192
{this.props.render(this.state)}
193
193
</div>
@@ -199,7 +199,7 @@ class MouseTracker extends React.Component {
199
199
render() {
200
200
return (
201
201
<div>
202
-
<h1>Move the mouse around!</h1>
202
+
<h1>Porusz myszką!</h1>
203
203
<Mouse render={mouse=> (
204
204
<Cat mouse={mouse} />
205
205
)}/>
@@ -209,17 +209,17 @@ class MouseTracker extends React.Component {
209
209
}
210
210
```
211
211
212
-
Now, instead of effectively cloning the `<Mouse>`component and hard-coding something else in its `render`method to solve for a specific use case, we provide a `render` prop that`<Mouse>`can use to dynamically determine what it renders.
212
+
Teraz, zamiast faktycznie klonować komponent `<Mouse>`i na stałe wpisywać coś innego w jego metodzie `render`dla poszczególnych przypadków, dostarczamy właściwość `render`, której komponent`<Mouse>`może użyć do dynamicznego określenia renderowanego wyniku.
213
213
214
-
More concretely, **a render prop is a function prop that a component uses to know what to render.**
214
+
Konkretnie rzecz ujmując, **właściwość renderująca jest funkcją, której komponent używa, aby wiedzieć, co ma wyrenderować.**
215
215
216
-
This technique makes the behavior that we need to share extremely portable. To get that behavior, render a`<Mouse>`with a `render` prop that tells it what to render with the current (x, y) of the cursor.
216
+
Ta technika powoduje, że zachowanie, które chcemy współdzielić, staje się niezwykle przenośne. Aby uzyskać zamierzony efekt, wyrenderuj komponent`<Mouse>`wraz z właściwością `render`, która określi, co powinno zostać wyrenderowane biorąc pod uwagę aktualną pozycję (x, y) kursora myszy.
217
217
218
-
One interesting thing to note about render props is that you can implement most [higher-order components](/docs/higher-order-components.html)(HOC) using a regular component with a render prop. For example, if you would prefer to have a`withMouse`HOC instead of a `<Mouse>` component, you could easily create one using a regular `<Mouse>`with a render prop:
218
+
Ciekawostką, o której warto wspomnieć podczas opisywania właściwości renderujących, jest to, że większość [komponentów wyższego rzędu (ang. *higher-order components*, *HOC*)](/docs/higher-order-components.html)można zaimplementować przy użyciu zwykłego komponentu z właściwością renderującą. Dla przykładu, jeżeli wolisz korzystać z komponentu wyższego rzędu`withMouse`zamiast komponentu `<Mouse>`, możesz z łatwością go stworzyć przy użyciu `<Mouse>`z właściwością renderującą:
219
219
220
220
```js
221
-
//If you really want a HOC for some reason, you can easily
222
-
//create one using a regular component with a render prop!
221
+
//Jeżeli z jakiegoś powodu naprawdę potrzebujesz HOC-a, możesz go łatwo
222
+
//stworzyć używając zwykłego komponentu z właściwością renderującą!
223
223
functionwithMouse(Component) {
224
224
returnclassextendsReact.Component {
225
225
render() {
@@ -233,62 +233,62 @@ function withMouse(Component) {
233
233
}
234
234
```
235
235
236
-
So using a render prop makes it possible to use either pattern.
236
+
Użycie właściwości renderującej powoduje, że jest możliwe użycie obu wzorców.
237
237
238
-
## Using Props Other Than`render` {#using-props-other-than-render}
238
+
## Używanie właściwości innych niż`render` {#using-props-other-than-render}
239
239
240
-
It's important to remember that just because the pattern is called "render props" you don't *have to use a prop named `render` to use this pattern*. In fact,[*any* prop that is a function that a component uses to know what to render is technically a "render prop"](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce).
240
+
Pamiętaj, że pomimo iż wzorzec nosi nazwę "właściwości renderującej" (ang. *render prop*), nie musisz *używać właściwości o nazwie `render`, aby go zastosować*. W zasadzie[*każda* właściwość, która jest funkcją służącą do określenia, co powinno zostać wyrenderowane, jest technicznie rzecz ujmując "właściwością renderującą"](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce).
241
241
242
-
Although the examples above use `render`, we could just as easily use the `children` prop!
242
+
Pomimo że powyższe przykłady używają nazwy `render`, moglibyśmy po prostu użyć właściwości `children`!
243
243
244
244
```js
245
245
<Mouse children={mouse=> (
246
-
<p>The mouse position is {mouse.x}, {mouse.y}</p>
246
+
<p>Pozycja kursora to {mouse.x}, {mouse.y}</p>
247
247
)}/>
248
248
```
249
249
250
-
And remember, the `children`prop doesn't actually need to be named in the list of "attributes" in your JSX element. Instead, you can put it directly *inside* the element!
250
+
Pamiętaj, że w JSX właściwości `children`nie musisz przekazywać na liście "atrybutów" twojego elementu. Zamiast tego możesz ją umieścić bezpośrednio *wewnątrz* znacznika!
251
251
252
252
```js
253
253
<Mouse>
254
254
{mouse=> (
255
-
<p>The mouse position is {mouse.x}, {mouse.y}</p>
255
+
<p>Pozycja kursora to {mouse.x}, {mouse.y}</p>
256
256
)}
257
257
</Mouse>
258
258
```
259
259
260
-
You'll see this technique used in the [react-motion](https://github.com/chenglou/react-motion) API.
260
+
Powyższa technika stosowana jest w bibliotece [react-motion](https://github.com/chenglou/react-motion).
261
261
262
-
Since this technique is a little unusual, you'll probably want to explicitly state that `children` should be a function in your `propTypes`when designing an API like this.
262
+
Jako że ta technika wydaje się trochę niecodzienna, podczas projektowania swojego API prawdopodobnie zechcesz jawnie określić w `propTypes`komponentu, że właściwość `children` powinna być funkcją.
263
263
264
264
```js
265
265
Mouse.propTypes= {
266
266
children:PropTypes.func.isRequired
267
267
};
268
268
```
269
269
270
-
## Caveats {#caveats}
270
+
## Ostrzeżenia {#caveats}
271
271
272
-
### Be careful when using Render Props with React.PureComponent {#be-careful-when-using-render-props-with-reactpurecomponent}
272
+
### Uważaj przy stosowaniu właściwości renderującej dla komponentów dziedziczących po React.PureComponent {#be-careful-when-using-render-props-with-reactpurecomponent}
273
273
274
-
Using a render prop can negate the advantage that comes from using[`React.PureComponent`](/docs/react-api.html#reactpurecomponent) if you create the function inside a `render` method. This is because the shallow prop comparison will always return `false` for new props, and each `render`in this case will generate a new value for the render prop.
274
+
Jeżeli stworzysz funkcję wewnątrz metody `render`, użycie właściwości renderującej może zniwelować korzyści płynące z użycia[`React.PureComponent`](/docs/react-api.html#reactpurecomponent). Dzieje się tak ponieważ płytkie porównanie (ang. *shallow comparison*) dla nowych właściwości zawsze będzie zwracać wartość `false`, a w tym przypadku każde wywołanie `render`będzie generować nową wartość dla właściwości renderującej.
275
275
276
-
For example, continuing with our`<Mouse>` component from above, if`Mouse`were to extend `React.PureComponent`instead of `React.Component`, our example would look like this:
276
+
Dla przykładu, kontynuując z komponentem`<Mouse>`, jeżeli`Mouse`rozszerzałaby klasę `React.PureComponent`zamiast `React.Component`, nasz przykład wyglądałby tak:
277
277
278
278
```js
279
279
classMouseextendsReact.PureComponent {
280
-
//Same implementation as above...
280
+
//Ta sama implementacja co wyżej...
281
281
}
282
282
283
283
classMouseTrackerextendsReact.Component {
284
284
render() {
285
285
return (
286
286
<div>
287
-
<h1>Move the mouse around!</h1>
287
+
<h1>Porusz myszką!</h1>
288
288
289
289
{/*
290
-
This is bad! The value of the `render` prop will
291
-
be different on each render.
290
+
Źle! Wartość właściwości `render` będzie
291
+
inna przy każdym wywołaniu metody render.
292
292
*/}
293
293
<Mouse render={mouse=> (
294
294
<Cat mouse={mouse} />
@@ -299,27 +299,27 @@ class MouseTracker extends React.Component {
299
299
}
300
300
```
301
301
302
-
In this example, each time `<MouseTracker>`renders, it generates a new function as the value of the `<Mouse render>` prop, thus negating the effect of `<Mouse>` extending`React.PureComponent`in the first place!
302
+
W tym przykładzie, za każdym razem gdy `<MouseTracker>`jest renderowany, generowana jest nowa funkcja jako wartość dla właściwości `<Mouse render>`. Zatem w pierwszej kolejności neguje to efekt rozszerzania`React.PureComponent`przez komponent `<Mouse>`!
303
303
304
-
To get around this problem, you can sometimes define the prop as an instance method, like so:
304
+
Aby obejść ten problem, możesz zdefiniować właściwość jako metodę instancji klasy:
305
305
306
306
```js
307
307
classMouseTrackerextendsReact.Component {
308
-
//Defined as an instance method, `this.renderTheCat` always
309
-
//refers to *same* function when we use it in render
308
+
//Zdefiniowana jako metoda instancji, `this.renderTheCat` zawsze
309
+
//odnosi się do *tej samej* funkcji podczas użycia w metodzie render
310
310
renderTheCat(mouse) {
311
311
return<Cat mouse={mouse} />;
312
312
}
313
313
314
314
render() {
315
315
return (
316
316
<div>
317
-
<h1>Move the mouse around!</h1>
317
+
<h1>Porusz myszką!</h1>
318
318
<Mouse render={this.renderTheCat} />
319
319
</div>
320
320
);
321
321
}
322
322
}
323
323
```
324
324
325
-
In cases where you cannot define the prop statically (e.g. because you need to close over the component's props and/or state)`<Mouse>`should extend `React.Component` instead.
325
+
W przypadkach gdy nie możesz zdefiniować właściwości statycznej (na przykład, dlatego że musisz domknąć właściwości komponentu i/lub jego stan),`<Mouse>`powinien rozszerzać klasę `React.Component`.
0 commit comments