Skip to content

Commit 0879be9

Browse files
bartlomiejzuberjakubdrozdek
authored andcommitted
Translate 'Render Props' page (#106)
1 parent b88e933 commit 0879be9

File tree

1 file changed

+59
-59
lines changed

1 file changed

+59
-59
lines changed

content/docs/render-props.md

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
---
22
id: render-props
3-
title: Render Props
3+
title: Właściwość renderująca
44
permalink: docs/render-props.html
55
---
66

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

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

1111
```jsx
1212
<DataProvider render={data => (
13-
<h1>Hello {data.target}</h1>
13+
<h1>Cześć, {data.target}</h1>
1414
)}/>
1515
```
1616

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).
1818

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

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}
2222

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

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

2727
```js
2828
class MouseTracker extends React.Component {
@@ -42,22 +42,22 @@ class MouseTracker extends React.Component {
4242
render() {
4343
return (
4444
<div style={{ height: '100%' }} onMouseMove={this.handleMouseMove}>
45-
<h1>Move the mouse around!</h1>
46-
<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>
4747
</div>
4848
);
4949
}
5050
}
5151
```
5252

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>`.
5454

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?
5656

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

5959
```js
60-
// The <Mouse> component encapsulates the behavior we need...
60+
// Komponent <Mouse> posiada funkcjonalność, której potrzebujemy...
6161
class Mouse extends React.Component {
6262
constructor(props) {
6363
super(props);
@@ -76,7 +76,7 @@ class Mouse extends React.Component {
7676
return (
7777
<div style={{ height: '100%' }} onMouseMove={this.handleMouseMove}>
7878

79-
{/* ...but how do we render something other than a <p>? */}
79+
{/* ...ale jak wyrenderować coś innego niż <p>? */}
8080
<p>The current mouse position is ({this.state.x}, {this.state.y})</p>
8181
</div>
8282
);
@@ -87,19 +87,19 @@ class MouseTracker extends React.Component {
8787
render() {
8888
return (
8989
<div>
90-
<h1>Move the mouse around!</h1>
90+
<h1>Porusz myszką!</h1>
9191
<Mouse />
9292
</div>
9393
);
9494
}
9595
}
9696
```
9797

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

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

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

104104
```js
105105
class Cat extends React.Component {
@@ -130,10 +130,10 @@ class MouseWithCat extends React.Component {
130130
<div style={{ height: '100%' }} onMouseMove={this.handleMouseMove}>
131131

132132
{/*
133-
We could just swap out the <p> for a <Cat> here ... but then
134-
we would need to create a separate <MouseWithSomethingElse>
135-
component every time we need to use it, so <MouseWithCat>
136-
isn't really reusable yet.
133+
Moglibyśmy w tym miejscu po prostu zamienić <p> na <Cat>... ale później
134+
musielibyśmy stworzyć oddzielny komponent <MouseWithSomethingElse>
135+
dla każdego przypadku użycia, dlatego też <MouseWithCat>
136+
nie jest jeszcze w pełni używalny wielokrotnie.
137137
*/}
138138
<Cat mouse={this.state} />
139139
</div>
@@ -145,17 +145,17 @@ class MouseTracker extends React.Component {
145145
render() {
146146
return (
147147
<div>
148-
<h1>Move the mouse around!</h1>
148+
<h1>Porusz myszką!</h1>
149149
<MouseWithCat />
150150
</div>
151151
);
152152
}
153153
}
154154
```
155155

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

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

160160
```js
161161
class Cat extends React.Component {
@@ -186,8 +186,8 @@ class Mouse extends React.Component {
186186
<div style={{ height: '100%' }} onMouseMove={this.handleMouseMove}>
187187

188188
{/*
189-
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ć.
191191
*/}
192192
{this.props.render(this.state)}
193193
</div>
@@ -199,7 +199,7 @@ class MouseTracker extends React.Component {
199199
render() {
200200
return (
201201
<div>
202-
<h1>Move the mouse around!</h1>
202+
<h1>Porusz myszką!</h1>
203203
<Mouse render={mouse => (
204204
<Cat mouse={mouse} />
205205
)}/>
@@ -209,17 +209,17 @@ class MouseTracker extends React.Component {
209209
}
210210
```
211211

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

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ć.**
215215

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

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ą:
219219

220220
```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ą!
223223
function withMouse(Component) {
224224
return class extends React.Component {
225225
render() {
@@ -233,62 +233,62 @@ function withMouse(Component) {
233233
}
234234
```
235235

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

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}
239239

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).
241241

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`!
243243

244244
```js
245245
<Mouse children={mouse => (
246-
<p>The mouse position is {mouse.x}, {mouse.y}</p>
246+
<p>Pozycja kursora to {mouse.x}, {mouse.y}</p>
247247
)}/>
248248
```
249249

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!
251251

252252
```js
253253
<Mouse>
254254
{mouse => (
255-
<p>The mouse position is {mouse.x}, {mouse.y}</p>
255+
<p>Pozycja kursora to {mouse.x}, {mouse.y}</p>
256256
)}
257257
</Mouse>
258258
```
259259

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).
261261

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ą.
263263

264264
```js
265265
Mouse.propTypes = {
266266
children: PropTypes.func.isRequired
267267
};
268268
```
269269

270-
## Caveats {#caveats}
270+
## Ostrzeżenia {#caveats}
271271

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}
273273

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

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

278278
```js
279279
class Mouse extends React.PureComponent {
280-
// Same implementation as above...
280+
// Ta sama implementacja co wyżej...
281281
}
282282

283283
class MouseTracker extends React.Component {
284284
render() {
285285
return (
286286
<div>
287-
<h1>Move the mouse around!</h1>
287+
<h1>Porusz myszką!</h1>
288288

289289
{/*
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.
292292
*/}
293293
<Mouse render={mouse => (
294294
<Cat mouse={mouse} />
@@ -299,27 +299,27 @@ class MouseTracker extends React.Component {
299299
}
300300
```
301301

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>`!
303303

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

306306
```js
307307
class MouseTracker extends React.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
310310
renderTheCat(mouse) {
311311
return <Cat mouse={mouse} />;
312312
}
313313

314314
render() {
315315
return (
316316
<div>
317-
<h1>Move the mouse around!</h1>
317+
<h1>Porusz myszką!</h1>
318318
<Mouse render={this.renderTheCat} />
319319
</div>
320320
);
321321
}
322322
}
323323
```
324324

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

Comments
 (0)