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
## Deklarowanie domyślnych wartości dla właściwości {#declaring-default-props}
32
32
33
-
With functions and ES6 classes `defaultProps`is defined as a property on the component itself:
33
+
W przypadku klas ES6 wartości dla `defaultProps`są definiowane na komponencie:
34
34
35
35
```javascript
36
36
classGreetingextendsReact.Component {
37
37
// ...
38
38
}
39
39
40
40
Greeting.defaultProps= {
41
-
name:'Mary'
41
+
name:'Maria'
42
42
};
43
43
```
44
44
45
-
With `createReactClass()`, you need to define `getDefaultProps()`as a function on the passed object:
45
+
Jeśli jednak korzystasz z `createReactClass()`, musisz zadeklarować w tym celu funkcję `getDefaultProps()`jako metodę przekazywanego obiektu:
46
46
47
47
```javascript
48
48
var Greeting =createReactClass({
49
49
getDefaultProps:function() {
50
50
return {
51
-
name:'Mary'
51
+
name:'Maria'
52
52
};
53
53
},
54
54
@@ -57,9 +57,9 @@ var Greeting = createReactClass({
57
57
});
58
58
```
59
59
60
-
## Setting the Initial State {#setting-the-initial-state}
60
+
## Ustawianie stanu początkowego {#setting-the-initial-state}
61
61
62
-
In ES6 classes, you can define the initial state by assigning `this.state` in the constructor:
62
+
W klasach ES6 definicja stanu początkowego następuje po przypisaniu w konstruktorze wartości do `this.state`:
63
63
64
64
```javascript
65
65
classCounterextendsReact.Component {
@@ -71,7 +71,7 @@ class Counter extends React.Component {
71
71
}
72
72
```
73
73
74
-
With`createReactClass()`, you have to provide a separate `getInitialState` method that returns the initial state:
74
+
W`createReactClass()` musisz przekazać osobną metodę `getInitialState`, która zwróci stan początkowy komponentu:
75
75
76
76
```javascript
77
77
var Counter =createReactClass({
@@ -82,16 +82,16 @@ var Counter = createReactClass({
82
82
});
83
83
```
84
84
85
-
## Autobinding {#autobinding}
85
+
## Automatyczne wiązanie {#autobinding}
86
86
87
-
In React components declared as ES6 classes, methods follow the same semantics as regular ES6 classes. This means that they don't automatically bind`this`to the instance. You'll have to explicitly use `.bind(this)`in the constructor:
87
+
W komponentach reactowych napisanych przy użyciu klas ES6, metody podlegają tym samym zasadom, co metody w zwykłych klasach ES6. Oznacza to, że nie dowiązują one automatycznie`this`do instancji. Musisz jawnie wywołać `.bind(this)`w konstruktorze:
88
88
89
89
```javascript
90
90
classSayHelloextendsReact.Component {
91
91
constructor(props) {
92
92
super(props);
93
-
this.state= {message:'Hello!'};
94
-
//This line is important!
93
+
this.state= {message:'Witaj!'};
94
+
//Ta linia jest istotna!
95
95
this.handleClick=this.handleClick.bind(this);
96
96
}
97
97
@@ -100,22 +100,23 @@ class SayHello extends React.Component {
100
100
}
101
101
102
102
render() {
103
-
// Because `this.handleClick` is bound, we can use it as an event handler.
103
+
// Ponieważ metoda `this.handleClick` jest dowiązana,
104
+
// możemy jej użyć jako procedurę obsługi zdarzeń.
104
105
return (
105
106
<button onClick={this.handleClick}>
106
-
Say hello
107
+
Przywitaj się
107
108
</button>
108
109
);
109
110
}
110
111
}
111
112
```
112
113
113
-
With `createReactClass()`, this is not necessary because it binds all methods:
114
+
W przypadku `createReactClass()` nie jest to wymagane, gdyż funkcja ta automatycznie dowiązuje wszystkie metody:
114
115
115
116
```javascript
116
117
var SayHello =createReactClass({
117
118
getInitialState:function() {
118
-
return {message:'Hello!'};
119
+
return {message:'Witaj!'};
119
120
},
120
121
121
122
handleClick:function() {
@@ -125,61 +126,61 @@ var SayHello = createReactClass({
125
126
render:function() {
126
127
return (
127
128
<button onClick={this.handleClick}>
128
-
Say hello
129
+
Przywitaj się
129
130
</button>
130
131
);
131
132
}
132
133
});
133
134
```
134
135
135
-
This means writing ES6 classes comes with a little more boilerplate code for event handlers, but the upside is slightly better performance in large applications.
136
+
Oznacza to, że korzystanie z klas ES6 wiąże się pisaniem więcej powtarzalnego kodu dla procedur obsługi zdarzeń, jednak na korzyść przemawia znacznie lepsza wydajność w dużych aplikacjach.
136
137
137
-
If the boilerplate code is too unattractive to you, you may enable the**experimental**[Class Properties](https://babeljs.io/docs/plugins/transform-class-properties/) syntax proposal with Babel:
138
+
Jeśli nie podoba ci się ten nadmiarowy kod, możesz włączyć w Babelu**eksperymentalną**składnię [właściwości klas (ang. *class properties*)](https://babeljs.io/docs/plugins/transform-class-properties/):
138
139
139
140
140
141
```javascript
141
142
classSayHelloextendsReact.Component {
142
143
constructor(props) {
143
144
super(props);
144
-
this.state= {message:'Hello!'};
145
+
this.state= {message:'Witaj!'};
145
146
}
146
-
//WARNING: this syntax is experimental!
147
-
//Using an arrow here binds the method:
147
+
//UWAGA: ten zapis jest jeszcze w fazie eksperymentalnej!
148
+
//Użycie funkcji strzałkowej powoduje automatycznie dowiązanie:
148
149
handleClick= () => {
149
150
alert(this.state.message);
150
151
}
151
152
152
153
render() {
153
154
return (
154
155
<button onClick={this.handleClick}>
155
-
Say hello
156
+
Przywitaj się
156
157
</button>
157
158
);
158
159
}
159
160
}
160
161
```
161
162
162
-
Please note that the syntax above is **experimental** and the syntax may change, or the proposal might not make it into the language.
163
+
Pamiętaj jednak, że powyższa składnia jest **eksperymentalna**, co oznacza, że może się zmienić lub zostać odrzucona i nie dodana do języka JavaScript.
163
164
164
-
If you'd rather play it safe, you have a few options:
165
+
Jeśli wolisz pewniejsze rozwiązania, masz kilka opcji:
165
166
166
-
*Bind methods in the constructor.
167
-
*Use arrow functions, e.g. `onClick={(e) => this.handleClick(e)}`.
168
-
*Keep using`createReactClass`.
167
+
*Dowiązuj metody w konstruktorze.
168
+
*Używaj funkcji strzałkowych, np. `onClick={(e) => this.handleClick(e)}`.
169
+
*Skorzystaj z`createReactClass`.
169
170
170
-
## Mixins {#mixins}
171
+
## Mixiny {#mixins}
171
172
172
-
>**Note:**
173
+
>**Uwaga:**
173
174
>
174
-
>ES6 launched without any mixin support. Therefore, there is no support for mixins when you use React with ES6 classes.
175
+
>Standard ES6 nie wspiera mixinów, dlatego domyślnie React nie będzie działał z mixinami użytymi w klasach ES6.
175
176
>
176
-
>**We also found numerous issues in codebases using mixins, [and don't recommend using them in the new code](/blog/2016/07/13/mixins-considered-harmful.html).**
177
+
>**Z naszych obserwacji wynika też, że używanie ich często powoduje problemy, [dlatego odradzamy korzystania z nich w nowym kodzie](/blog/2016/07/13/mixins-considered-harmful.html).**
177
178
>
178
-
>This section exists only for the reference.
179
+
>Ten rozdział istnieje tylko dla zapewnienia kompletności dokumentacji.
179
180
180
-
Sometimes very different components may share some common functionality. These are sometimes called [cross-cutting concerns](https://en.wikipedia.org/wiki/Cross-cutting_concern). `createReactClass`lets you use a legacy `mixins` system for that.
181
+
Niekiedy różne komponenty mogą współdzielić te same funkcjonalności. Nazywa się je także [problemami przekrojowymi](https://en.wikipedia.org/wiki/Cross-cutting_concern). `createReactClass`pozwala na zastosowanie w tym celu przestarzałego systemu `mixinów`.
181
182
182
-
One common use case is a component wanting to update itself on a time interval. It's easy to use `setInterval()`, but it's important to cancel your interval when you don't need it anymore to save memory. React provides [lifecycle methods](/docs/react-component.html#the-component-lifecycle) that let you know when a component is about to be created or destroyed. Let's create a simple mixin that uses these methods to provide an easy `setInterval()` function that will automatically get cleaned up when your component is destroyed.
183
+
Jednym z częstych przykładów jest komponent, które chce aktualizować swój stan w równych odstępach czasu. Łatwo jest skorzystać z funkcji `setInterval()`, lecz należy pamiętać o anulowaniu interwału, gdy już nie jest potrzebny, aby zwolnić pamięć. React dostarcza [metody cyklu życia](/docs/react-component.html#the-component-lifecycle), które informują o tym, kiedy komponent jest tworzony lub niszczony. Stwórzmy prosty mixin, korzystający z tych metod, udostępniający prostą funkcję `setInterval()`, która będzie automatycznie po sobie "sprzątała", gdy komponent ulegnie zniszczeniu.
183
184
184
185
```javascript
185
186
var SetIntervalMixin = {
@@ -197,20 +198,20 @@ var SetIntervalMixin = {
197
198
var createReactClass =require('create-react-class');
198
199
199
200
var TickTock =createReactClass({
200
-
mixins: [SetIntervalMixin], //Use the mixin
201
+
mixins: [SetIntervalMixin], //Użyj mixinu
201
202
getInitialState:function() {
202
203
return {seconds:0};
203
204
},
204
205
componentDidMount:function() {
205
-
this.setInterval(this.tick, 1000); //Call a method on the mixin
206
+
this.setInterval(this.tick, 1000); //Wywołaj metodę z mixinu
206
207
},
207
208
tick:function() {
208
209
this.setState({seconds:this.state.seconds+1});
209
210
},
210
211
render:function() {
211
212
return (
212
213
<p>
213
-
React has been running for{this.state.seconds} seconds.
214
+
React jest już uruchomiony {this.state.seconds} sekund.
214
215
</p>
215
216
);
216
217
}
@@ -222,4 +223,4 @@ ReactDOM.render(
222
223
);
223
224
```
224
225
225
-
If a component is using multiple mixins and several mixins define the same lifecycle method (i.e. several mixins want to do some cleanup when the component is destroyed), all of the lifecycle methods are guaranteed to be called. Methods defined on mixins run in the order mixins were listed, followed by a method call on the component.
226
+
Jeśli komponent używa kilku mixinów i niektóre z nich definiują te same metody cyklu życia (tj. kilka z nich chce posprzątać przed zniszczeniem komponentu), React gwarantuje, że wszystkie zostaną wywołane. Metody zdefiniowane w mixinach są uruchamiane zgodnie z kolejnością ich dodania, a na koniec uruchamiana jest metoda samego komponentu.
0 commit comments