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
A common pattern in React is for a component to return multiple elements. Fragments let you group a list of children without adding extra nodes to the DOM.
7
+
Często spotykanym wzorcem w Reakcie jest tworzenie komponentów, które zwracają wiele elementów. Fragmenty pozwalają zgrupować listę potomków bez konieczności dodawania zbędnych węzłów do drzewa DOM.
8
8
9
9
```js
10
10
render() {
@@ -18,11 +18,11 @@ render() {
18
18
}
19
19
```
20
20
21
-
There is also a new [short syntax](#short-syntax)for declaring them, but it isn't supported by all popular tools yet.
21
+
Istnieje również nowy [skrócony zapis](#short-syntax)do deklarowania fragmentów, jednak nie jest on jeszcze wspierany przez wszystkie popularne narzędzia.
22
22
23
-
## Motivation {#motivation}
23
+
## Motywacja {#motivation}
24
24
25
-
A common pattern is for a component to return a list of children. Take this example React snippet:
25
+
Zdarza się, że potrzebujemy w komponencie zwrócić listę potomków. Rozważmy poniższy przykład:
26
26
27
27
```jsx
28
28
classTableextendsReact.Component {
@@ -38,93 +38,93 @@ class Table extends React.Component {
38
38
}
39
39
```
40
40
41
-
`<Columns />`would need to return multiple `<td>` elements in order for the rendered HTML to be valid. If a parent div was used inside the `render()`of`<Columns />`, then the resulting HTML will be invalid.
41
+
Aby zapewnić poprawność wyrenderowanego kodu HTML, komponent `<Columns />`powinien zwrócić kilka elementów `<td>`. Gdyby komórki tabeli, zwracane przez funkcję `render()`komponentu`<Columns />`, otoczyć np. elementem `<div>`, powstały w ten sposób kod HTML byłby nieprawidłowy.
42
42
43
43
```jsx
44
44
classColumnsextendsReact.Component {
45
45
render() {
46
46
return (
47
47
<div>
48
-
<td>Hello</td>
49
-
<td>World</td>
48
+
<td>Witaj</td>
49
+
<td>Świecie</td>
50
50
</div>
51
51
);
52
52
}
53
53
}
54
54
```
55
55
56
-
results in a `<Table />` output of:
56
+
daje w rezultacie następującą strukturę dla komponentu `<Table />`:
57
57
58
58
```jsx
59
59
<table>
60
60
<tr>
61
61
<div>
62
-
<td>Hello</td>
63
-
<td>World</td>
62
+
<td>Witaj</td>
63
+
<td>Świecie</td>
64
64
</div>
65
65
</tr>
66
66
</table>
67
67
```
68
68
69
-
Fragments solve this problem.
69
+
Fragmenty rozwiązują ten problem.
70
70
71
-
## Usage {#usage}
71
+
## Użycie {#usage}
72
72
73
73
```jsx{4,7}
74
74
class Columns extends React.Component {
75
75
render() {
76
76
return (
77
77
<React.Fragment>
78
-
<td>Hello</td>
79
-
<td>World</td>
78
+
<td>Witaj</td>
79
+
<td>Świecie</td>
80
80
</React.Fragment>
81
81
);
82
82
}
83
83
}
84
84
```
85
85
86
-
which results in a correct `<Table />` output of:
86
+
daje w rezultacie następującą strukturę dla komponentu `<Table />`:
87
87
88
88
```jsx
89
89
<table>
90
90
<tr>
91
-
<td>Hello</td>
92
-
<td>World</td>
91
+
<td>Witaj</td>
92
+
<td>Świecie</td>
93
93
</tr>
94
94
</table>
95
95
```
96
96
97
-
### Short Syntax {#short-syntax}
97
+
### Skrócony zapis {#short-syntax}
98
98
99
-
There is a new, shorter syntax you can use for declaring fragments. It looks like empty tags:
99
+
Istnieje nowy, krótszy zapis służący do deklarowania fragmentów. Z wyglądu przypomina puste znaczniki:
100
100
101
101
```jsx{4,7}
102
102
class Columns extends React.Component {
103
103
render() {
104
104
return (
105
105
<>
106
-
<td>Hello</td>
107
-
<td>World</td>
106
+
<td>Witaj</td>
107
+
<td>Świecie</td>
108
108
</>
109
109
);
110
110
}
111
111
}
112
112
```
113
113
114
-
You can use `<></>`the same way you'd use any other element except that it doesn't support keys or attributes.
114
+
Możesz używać `<></>`tak samo, jak innych komponentów. Nie możesz jednak przekazywać do niego klucza (`key`) ani żadnych innych właściwości.
115
115
116
-
Note that **[many tools don't support it yet](/blog/2017/11/28/react-v16.2.0-fragment-support.html#support-for-fragment-syntax)** so you might want to explicitly write`<React.Fragment>` until the tooling catches up.
116
+
Zwróć uwagę, że **[wiele narzędzi nie posiada jeszcze wsparcia dla tego zapisu](/blog/2017/11/28/react-v16.2.0-fragment-support.html#support-for-fragment-syntax)**. Do czasu jego dodania pisz po prostu`<React.Fragment>`.
117
117
118
-
### Keyed Fragments {#keyed-fragments}
118
+
### Fragmenty z kluczem {#keyed-fragments}
119
119
120
-
Fragments declared with the explicit `<React.Fragment>`syntax may have keys. A use case for this is mapping a collection to an array of fragments -- for example, to create a description list:
120
+
Fragmenty zadeklarowane jawnie przy użyciu składni `<React.Fragment>`mogą posiadać klucze. Ma to zastosowanie, gdy zechcesz przemapować kolekcję na tablicę fragmentów -- na przykład do stworzenia listy opisów:
121
121
122
122
```jsx
123
123
functionGlossary(props) {
124
124
return (
125
125
<dl>
126
126
{props.items.map(item=> (
127
-
//Without the `key`, React will fire a key warning
127
+
//Bez `key` React wyrzuci błąd
128
128
<React.Fragment key={item.id}>
129
129
<dt>{item.term}</dt>
130
130
<dd>{item.description}</dd>
@@ -135,8 +135,8 @@ function Glossary(props) {
135
135
}
136
136
```
137
137
138
-
`key`is the only attribute that can be passed to `Fragment`. In the future, we may add support for additional attributes, such as event handlers.
138
+
`key`jest jedyną właściwością, którą można przekazać do fragmentu. Możliwe, że w przyszłości dodamy wsparcie dla innych właściwości, np. procedur obsługi zdarzeń.
139
139
140
-
### Live Demo {#live-demo}
140
+
### Demo {#live-demo}
141
141
142
-
You can try out the new JSX fragment syntax with this [CodePen](https://codepen.io/reactjs/pen/VrEbjE?editors=1000).
142
+
Możesz wypróbować nową składnię JSX dla fragmentów na tym [CodePenie](https://codepen.io/reactjs/pen/VrEbjE?editors=1000).
0 commit comments