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/conditional-rendering.md
+42-38Lines changed: 42 additions & 38 deletions
Original file line number
Diff line number
Diff line change
@@ -1,30 +1,30 @@
1
1
---
2
2
id: conditional-rendering
3
-
title: Conditional Rendering
3
+
title: Renderowanie warunkowe
4
4
permalink: docs/conditional-rendering.html
5
5
prev: handling-events.html
6
6
next: lists-and-keys.html
7
7
redirect_from:
8
8
- "tips/false-in-jsx.html"
9
9
---
10
10
11
-
In React, you can create distinct components that encapsulate behavior you need. Then, you can render only some of them, depending on the state of your application.
11
+
React umożliwia tworzenie odrębnych komponentów, które hermetyzują (ang. *encapsulate*) pożądane przez ciebie metody. Następnie zrenderowane mogą być wybrane komponenty, w zależności od stanu twojej aplikacji.
12
12
13
-
Conditional rendering in React works the same way conditions work in JavaScript. Use JavaScript operators like [`if`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else) or the [conditional operator](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) to create elements representing the current state, and let React update the UI to match them.
13
+
Renderowanie warunkowe działa w Reakcie tak samo jak warunki JavaScriptowe. Aby stworzyć elementy odzwierciedlające aktualny stan aplikacji, należy użyć operatora takiego jak [if](https://developer.mozilla.org/pl/docs/Learn/Getting_started_with_the_web/JavaScript_basics#Warunki) lub [operatora warunkowego](https://developer.mozilla.org/pl/docs/Web/JavaScript/Referencje/Operatory/Operator_warunkowy) oraz pozwolić Reactowi je dopasować poprzez aktualizację interfejsu użytkownika.
14
14
15
-
Consider these two components:
15
+
Rozważmy następujące dwa komponenty:
16
16
17
17
```js
18
18
functionUserGreeting(props) {
19
-
return<h1>Welcome back!</h1>;
19
+
return<h1>Witamy ponownie!</h1>;
20
20
}
21
21
22
22
functionGuestGreeting(props) {
23
-
return<h1>Please sign up.</h1>;
23
+
return<h1>Proszę się zapisać.</h1>;
24
24
}
25
25
```
26
26
27
-
We'll create a `Greeting`component that displays either of these components depending on whether a user is logged in:
27
+
Stworzymy komponent `Greeting`(ang. *Powitanie*), który wyświetlał będzie jeden lub drugi z powyższych komponentów w zależności od tego czy użytkownik jest zalogowany.
28
28
29
29
```javascript{3-7,11,12}
30
30
function Greeting(props) {
@@ -42,37 +42,38 @@ ReactDOM.render(
42
42
);
43
43
```
44
44
45
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/ZpVxNq?editors=0011)
45
+
[**Przetestuj kod na CodePen**](https://codepen.io/gaearon/pen/ZpVxNq?editors=0011)
46
46
47
-
This example renders a different greeting depending on the value of `isLoggedIn` prop.
47
+
Powitanie renderowane przez kod w powyższym przykładzie zależy of wartości właściwości `isLoggedIn`.
48
48
49
-
### Element Variables {#element-variables}
49
+
### Zmienne elementowe {#element-variables}
50
50
51
-
You can use variables to store elements. This can help you conditionally render a part of the component while the rest of the output doesn't change.
51
+
Elementy mogą być przechowywane w zmiennych. Pozwala to na warunkowe renderowanie określonej części, komponentu podczas gdy pozostałe dane wyjściowe nie ulegają zmianie.
52
52
53
-
Consider these two new components representing Logout and Login buttons:
53
+
Przyjrzyjmy się dwóm nowym komponentom tworzącym przyciski logowania Zaloguj się (ang. *Login*) oraz Wyloguj się (ang. *Logout*):
54
54
55
55
```js
56
56
functionLoginButton(props) {
57
57
return (
58
58
<button onClick={props.onClick}>
59
-
Login
59
+
Zaloguj się
60
60
</button>
61
61
);
62
62
}
63
63
64
64
functionLogoutButton(props) {
65
65
return (
66
66
<button onClick={props.onClick}>
67
-
Logout
67
+
Wyloguj się
68
68
</button>
69
69
);
70
70
}
71
71
```
72
72
73
-
In the example below, we will create a [stateful component](/docs/state-and-lifecycle.html#adding-local-state-to-a-class) called `LoginControl`.
73
+
W przykładzie poniżej zbudujemy [komponent ze stanem](/docs/state-and-lifecycle.html#adding-local-state-to-a-class) o nazwie `LoginControl` (pol. *kontrola logowania*)
74
+
75
+
W zależności od aktualnego stanu będzie on renderował przycisk logowania (`<LoginButton />`) lub wylogowania `<LogoutButton />` . Będzie on również renderował komponent `<Greeting />` z poprzedniego przykładu:
74
76
75
-
It will render either `<LoginButton />` or `<LogoutButton />` depending on its current state. It will also render a `<Greeting />` from the previous example:
76
77
77
78
```javascript{20-25,29,30}
78
79
class LoginControl extends React.Component {
@@ -116,23 +117,24 @@ ReactDOM.render(
116
117
);
117
118
```
118
119
119
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/QKzAgB?editors=0010)
120
+
[**Przetestuj kod na CodePen**](https://codepen.io/gaearon/pen/QKzAgB?editors=0010)
121
+
122
+
Deklarowanie zmiennej oraz stosowanie warunku `if` to dobry sposób na warunkowe renderowanie komponentu. Czasem jednak przydaje się nieco krótsza składnia. JSX umożliwia kilka różnych opcji warunków wewnątrz liniowych. Przedstawiamy je poniżej.
120
123
121
-
While declaring a variable and using an `if` statement is a fine way to conditionally render a component, sometimes you might want to use a shorter syntax. There are a few ways to inline conditions in JSX, explained below.
122
124
123
-
### Inline If with Logical && Operator {#inline-if-with-logical--operator}
125
+
### Wewnątrz liniowy warunek `if` z użyciem logicznego operatora `&&` {#inline-if-with-logical--operator}
124
126
125
-
You may [embed any expressions in JSX](/docs/introducing-jsx.html#embedding-expressions-in-jsx) by wrapping them in curly braces. This includes the JavaScript logical `&&` operator. It can be handy for conditionally including an element:
127
+
JSX umożliwia stosowanie w nawiasach klamrowych [wszelkich wyrażeń](/docs/introducing-jsx.html#embedding-expressions-in-jsx), łącznie z JavaScriptowym operatorem logicznym `&&`. Jest to przydatne do warunkowego załączania elementu.
[**Try it on CodePen**](https://codepen.io/gaearon/pen/ozJddz?editors=0010)
151
+
[**Przetestuj kod na CodePen**](https://codepen.io/gaearon/pen/ozJddz?editors=0010)
150
152
151
-
It works because in JavaScript, `true && expression` always evaluates to `expression`, and`false && expression` always evaluates to`false`.
153
+
Powyższy kod działa ponieważ w JavaScripcie `true && wyrażenie` zawsze jest ewaluowane jako `wyrażenie`, natomiast`false && wyrażenie` jako`false`.
152
154
153
-
Therefore, if the condition is `true`, the element right after `&&`will appear in the output. If it is `false`, React will ignore and skip it.
155
+
Zatem jeśli warunek jest `true`, element następujący bezpośrednio po `&&`zostanie ukazany w danych wyjściowych. Natomiast jeśli warunek jest `fałszywy`, React zignoruje go i pominie przy renderowaniu.
154
156
155
-
### Inline If-Else with Conditional Operator {#inline-if-else-with-conditional-operator}
157
+
### Warunek if-else z operatorem warunkowym {#inline-if-else-with-conditional-operator}
156
158
157
-
Another method for conditionally rendering elements inline is to use the JavaScript conditional operator [`condition ? true : false`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator).
159
+
Kolejną metodą renderowania warunkowego wewnątrz liniowego jest stosowanie JavaScriptowego operatora warunkowego [`warunek ? true : false`](https://developer.mozilla.org/pl/docs/Web/JavaScript/Referencje/Operatory/Operator_warunkowy).
158
160
159
-
In the example below, we use it to conditionally render a small block of text.
161
+
W przykładzie poniżej używamy go, aby warunkowo zrenderować mały blok tekstu.
160
162
161
163
```javascript{5}
162
164
render() {
163
165
const isLoggedIn = this.state.isLoggedIn;
164
166
return (
165
167
<div>
166
-
The user is <b>{isLoggedIn ? 'currently' : 'not'}</b> logged in.
168
+
Użytkownik jest teraz <b>{isLoggedIn ? 'zalogowany' : 'niezalogowany'}</b>.
167
169
</div>
168
170
);
169
171
}
170
172
```
171
173
172
-
It can also be used for larger expressions although it is less obvious what's going on:
174
+
Rozwiązanie to może być stosowane również w przypadku dłuższych wyrażeń:
173
175
174
176
```js{5,7,9}
175
177
render() {
@@ -185,14 +187,16 @@ render() {
185
187
);
186
188
}
187
189
```
190
+
Czytelność takich wyrażeń jest oczywiście nieco mniejsza. Podobnie jak w JavaScripcie, wybór odpowiedniego stylu zależy od preferencji twoich i twojego zespołu. Jednocześnie należy pamiętać, że kiedy warunki stają się nazbyt złożone, dobrze jest roważyć możliwość [stworzenia osobnego komponentu](/docs/components-and-props.html#extracting-components)
Just like in JavaScript, it is up to you to choose an appropriate style based on what you and your team consider more readable. Also remember that whenever conditions become too complex, it might be a good time to [extract a component](/docs/components-and-props.html#extracting-components).
195
+
W sporadycznych przypadkach, może zachodzić potrzeba ukrycia się komponentu mimo, iż został on zrenderowany przez inny komponent. Aby to umożliwić należy zastosować wartość `null` zamiast jego renderowanych danych wyjściowych.
190
196
191
-
### Preventing Component from Rendering {#preventing-component-from-rendering}
192
197
193
-
In rare cases you might want a component to hide itself even though it was rendered by another component. To do this return `null` instead of its render output.
198
+
W przykładzie poniżej, renderowanie baneru ostrzegawczego `<WarningBanner />` jest uzależnione od wartości właściwości o nazwie `warn` (ang. *ostrzeż*). Jeśli wartość tej właściwości jest `false`, wówczas komponent ten nie jest renderowany.
194
199
195
-
In the example below, the `<WarningBanner />` is rendered depending on the value of the prop called `warn`. If the value of the prop is `false`, then the component does not render:
196
200
197
201
```javascript{2-4,29}
198
202
function WarningBanner(props) {
@@ -202,7 +206,7 @@ function WarningBanner(props) {
202
206
203
207
return (
204
208
<div className="warning">
205
-
Warning!
209
+
Ostrzeżenie!
206
210
</div>
207
211
);
208
212
}
@@ -225,7 +229,7 @@ class Page extends React.Component {
225
229
<div>
226
230
<WarningBanner warn={this.state.showWarning} />
227
231
<button onClick={this.handleToggleClick}>
228
-
{this.state.showWarning ? 'Hide' : 'Show'}
232
+
{this.state.showWarning ? 'Ukryj' : 'Pokaż'}
229
233
</button>
230
234
</div>
231
235
);
@@ -238,6 +242,6 @@ ReactDOM.render(
238
242
);
239
243
```
240
244
241
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/Xjoqwm?editors=0010)
245
+
[**Przetestuj kod na CodePen**](https://codepen.io/gaearon/pen/Xjoqwm?editors=0010)
242
246
243
-
Returning `null`from a component's `render`method does not affect the firing of the component's lifecycle methods. For instance `componentDidUpdate`will still be called.
247
+
Zwrócenie wartości `null`z metody `render`danego komponentu nie ma wpływu na wywoływanie metod cyklu życia tego komponentu. To znaczy, że np. metoda `componentDidUpdate`w dalszym ciągu zostanie wywołana.
0 commit comments