Skip to content

Commit a4dfc33

Browse files
render-props.md translation
1 parent 3dae243 commit a4dfc33

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 Reacta, 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 renderowaną właściwością, przyjmuje funkcję która zwraca element Reacta a następnie wywołuje ją, nie implementując jednocześnie logiki do wyrenderowania 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 które używają 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 dlaczego renderowanie właściwości jest przydatne i jak napisać je samemu.
2020

21-
## Use Render Props for Cross-Cutting Concerns {#use-render-props-for-cross-cutting-concerns}
21+
## Użycie Właściwości Renderującej do Zagadnień-przecinających(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ą podstawową jednostką reużywalności kodu w Reakcie, jednak nie zawsze oczywistością jest jak współdzielić stan lub zachowanie enkapsulowane przez jeden komponent, podczas gdy inne komponenty mogą potrzebować tego samego stanu.
2424

25-
For example, the following component tracks the mouse position in a web app:
25+
Dla przykładu, poniższy komponent śledzi pozycje myszki w aplikacji webowej:
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 myszki 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 `<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 wiedzieć o pozycji kursora, czy możemy enkapsulować to zachowanie tak abyśmy mogli łatwo współdzielić 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+
Odkąd komponenty są podstawową jednostką reużywalności w Reakcie, spróbujmy zrefaktoryzować trochę kod, aby użyć komponentu `<Mouse>` który enkapsuluje zachowanie które musimy użyć gdzieś indziej.
5858

5959
```js
60-
// The <Mouse> component encapsulates the behavior we need...
60+
// Komponent <Mouse> enkapsuluje zachowanie którego 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>` enkapsuluje wszystkie zachowania związane z nasłuchiwaniem na zdarzenia `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 myszkę 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>`*, 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+
za każdym razem gdy chcielibyśmy go użyć, dlatego też <MouseWithCat>
136+
nie jest jeszcze naprawdę reużywalny.
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 enkapsulacja zachowania w taki sposób aby było ono reużywalne. Teraz, za każdym razem gdy potrzebowalibyśmy pozycji myszki dla innego przypadku, musimy stworzyć nowy komponent (innymi słowy kolejny `<MouseWithCat>`) który wyrenderuje coś konkretnie dla przypadku użycia.
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+
To miejsce w którym renderowanie właściwości przychodzi z odsieczą. Zamiast na trwało zakodować (ang. *hard-coding*) `<Cat>` wewnątrz komponentu `<Mouse>` i zmieniać w ten sposób to co zostało wyrenderowane, możemy dostarczyć do komponentu `<Mouse>` właściwość będącą funkcją dynamiczne określającą 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 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 trwale zakodowywać coś innego w jego metodzie `render` aby rozwiązać specyficzny przypadek użycia, dostarczamy właściwość `render`, którą komponent `<Mouse>` może użyć do dynamicznego określenia co ma zostać wyrenderowane.
213213

214-
More concretely, **a render prop is a function prop that a component uses to know what to render.**
214+
Bardziej konkretnie, **właściwość renderująca jest funkcją której komponent używa aby wiedzieć co 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 dostać to zachowanie, wyrenderuj komponent `<Mouse>` wraz z właściwością `render`, która mówi co ma zostać wyrenderowane z aktualną pozycją (x, y) kursora.
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+
Jedną interesującą rzeczą o której warto wspomnieć podczas opisywania właściwości renderujących jest to że możesz zaimplementować większość [komponentów wyższego rzędu(ang. *higher-order components*)](/docs/higher-order-components.html) (HOC) używając zwykłego komponentu z właściwością renderującą. Dla przykładu, jeżeli preferujesz wykorzystać komponent wyższego rzędu `withMouse` zamiast komponentu `<Mouse>`, możesz z łatwością go stworzyć używając `<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ę chcesz HOC, 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+
Ważne jest aby pamiętać że pomimo iż wzorzec jest nazywany "właściowość renderująca(ang. *render props*)" to nie musisz *używać właściowści o nazwanej `render` aby go stosować*. W zasadzie, [*każda* właściwość która jest funkcją, której komponent używa aby wiedzieć co wyrenderować jest technicznie "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ą `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 myszki 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łaściwość `children` nie musi być nazwana na liście "atrybutów" twojego elementu JSX. Zamiast tego, możesz ją umieścić bezpośrednio *wewnątrz* elementu!
251251

252252
```js
253253
<Mouse>
254254
{mouse => (
255-
<p>The mouse position is {mouse.x}, {mouse.y}</p>
255+
<p>Pozycja myszki 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+
Możesz zauważyć wykorzystanie tej techniki w [react-motion](https://github.com/chenglou/react-motion) API.
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+
Odkąd ta technika jest trochę niecodzienna, podczas projektowania swojego API, prawdopodobnie będziesz chciał jawnie określić w swoich `propTypes` że `children` powinien 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+
### Bądź ostrożny podczas używania Właściwości Renderujących z 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ściowości renderującej może anulować korzyści płynące z użycia [`React.PureComponent`](/docs/react-api.html#reactpurecomponent). Dzieje się tak ponieważ płytkie porównanie 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, kontynując z komponentem `<Mouse>`, jeżeli `Mouse` rozszerzałaby `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>`, a zatem neguje efekt `<Mouse>` rozszerzającej w pierwszej kolejności `React.PureComponent`!
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ściowść jako metodę instancji, w ten sposób:
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ć `React.Component`.

0 commit comments

Comments
 (0)