|
1 | 1 | ---
|
2 | 2 | id: faq-state
|
3 |
| -title: Component State |
| 3 | +title: Stan komponentu |
4 | 4 | permalink: docs/faq-state.html
|
5 | 5 | layout: docs
|
6 | 6 | category: FAQ
|
7 | 7 | ---
|
8 | 8 |
|
9 |
| -### What does `setState` do? {#what-does-setstate-do} |
| 9 | +### Co robi `setState`? {#what-does-setstate-do} |
10 | 10 |
|
11 |
| -`setState()` schedules an update to a component's `state` object. When state changes, the component responds by re-rendering. |
| 11 | +`setState()` tworzy plan aktualizacji obiektu `state` w komponencie. Gdy stan ulega zmianie, komponent reaguje poprzez ponowne renderowanie. |
12 | 12 |
|
13 |
| -### What is the difference between `state` and `props`? {#what-is-the-difference-between-state-and-props} |
| 13 | +### Jaka jest różnica między `state` a `props`? {#what-is-the-difference-between-state-and-props} |
14 | 14 |
|
15 |
| -[`props`](/docs/components-and-props.html) (short for "properties") and [`state`](/docs/state-and-lifecycle.html) are both plain JavaScript objects. While both hold information that influences the output of render, they are different in one important way: `props` get passed *to* the component (similar to function parameters) whereas `state` is managed *within* the component (similar to variables declared within a function). |
| 15 | +Właściwości [`props`](/docs/components-and-props.html) (skrót od *properties*) i stan [`state`](/docs/state-and-lifecycle.html) są zwykłymi obiektami javascriptowymi. Przechowują informacje, które wpływają na wynik renderowania komponentu, jednak jest między nimi istotna różnica: właściwości `props` są przekazywane *do* komponentu (podobnie jak argumenty do funkcji), podczas gdy stan `state` jest zarządzany *wewnątrz* komponentu (podobnie jak zmienna w ciele funkcji). |
16 | 16 |
|
17 |
| -Here are some good resources for further reading on when to use `props` vs `state`: |
18 |
| -* [Props vs State](https://github.com/uberVU/react-guide/blob/master/props-vs-state.md) |
19 |
| -* [ReactJS: Props vs. State](https://lucybain.com/blog/2016/react-state-vs-pros/) |
| 17 | +Oto kilka sprawdzonych źródeł, z których dowiesz się więcej o tym, kiedy używać właściwości `props`, a kiedy stanu `state`: |
| 18 | +* [Właściwości kontra stan](https://github.com/uberVU/react-guide/blob/master/props-vs-state.md) |
| 19 | +* [ReactJS: Właściwości kontra stan](https://lucybain.com/blog/2016/react-state-vs-pros/) |
20 | 20 |
|
21 |
| -### Why is `setState` giving me the wrong value? {#why-is-setstate-giving-me-the-wrong-value} |
| 21 | +### Dlaczego `setState` błędnie ustawia wartość? {#why-is-setstate-giving-me-the-wrong-value} |
22 | 22 |
|
23 |
| -In React, both `this.props` and `this.state` represent the *rendered* values, i.e. what's currently on the screen. |
| 23 | +W Reakcie zarówno `this.props`, jak i `this.state` reprezentują *wyrenderowane* wartości, tzn. te, które aktualnie widzimy na ekranie. |
24 | 24 |
|
25 |
| -Calls to `setState` are asynchronous - don't rely on `this.state` to reflect the new value immediately after calling `setState`. Pass an updater function instead of an object if you need to compute values based on the current state (see below for details). |
| 25 | +Wywołania funkcji `setState` są asynchroniczne - nie spodziewaj się, że `this.state` będzie odzwierciedlać aktualny stan natychmiast po wywołaniu `setState`. Jeśli chcesz obliczyć nowe wartości na podstawie starych, zamiast obiektu przekaż funkcję aktualizującą (więcej o tym poniżej). |
26 | 26 |
|
27 |
| -Example of code that will *not* behave as expected: |
| 27 | +Przykład kodu, który *nie* zachowa się tak, jak byśmy się spodziewali: |
28 | 28 |
|
29 | 29 | ```jsx
|
30 | 30 | incrementCount() {
|
31 |
| - // Note: this will *not* work as intended. |
| 31 | + // Uwaga: To *nie* zadziała tak, jak myślisz. |
32 | 32 | this.setState({count: this.state.count + 1});
|
33 | 33 | }
|
34 | 34 |
|
35 | 35 | handleSomething() {
|
36 |
| - // Let's say `this.state.count` starts at 0. |
| 36 | + // Załóżmy, że `this.state.count` startuje z wartością 0. |
37 | 37 | this.incrementCount();
|
38 | 38 | this.incrementCount();
|
39 | 39 | this.incrementCount();
|
40 |
| - // When React re-renders the component, `this.state.count` will be 1, but you expected 3. |
| 40 | + // Kiedy React ponownie wyrenderuje ten komponent, wartość `this.state.count` będzie 1, a nie 3. |
41 | 41 |
|
42 |
| - // This is because `incrementCount()` function above reads from `this.state.count`, |
43 |
| - // but React doesn't update `this.state.count` until the component is re-rendered. |
44 |
| - // So `incrementCount()` ends up reading `this.state.count` as 0 every time, and sets it to 1. |
| 42 | + // Dzieje się tak dlatego, że powyższa funkcja `incrementCount()` odczytuje wartość z `this.state.count`, |
| 43 | + // jednak React nie aktualizuje wartości `this.state.count`, dopóki nie nastąpi ponowne wyrenderowanie. |
| 44 | + // Dlatego `incrementCount()` za każdym razem odczyta `this.state.count` jako 0, a następnie zaplanuje zmianę na 1. |
45 | 45 |
|
46 |
| - // The fix is described below! |
| 46 | + // Jak sobie z tym poradzić? Czytaj dalej! |
47 | 47 | }
|
48 | 48 | ```
|
49 | 49 |
|
50 |
| -See below for how to fix this problem. |
| 50 | +Poniżej znajduje się rozwiązanie tego problemu. |
51 | 51 |
|
52 |
| -### How do I update state with values that depend on the current state? {#how-do-i-update-state-with-values-that-depend-on-the-current-state} |
| 52 | +### Jak aktualizować stan wartościami, które zależą od aktualnego stanu? {#how-do-i-update-state-with-values-that-depend-on-the-current-state} |
53 | 53 |
|
54 |
| -Pass a function instead of an object to `setState` to ensure the call always uses the most updated version of state (see below). |
| 54 | +Do `setState` zamiast obiektu przekaż funkcję, aby upewnić się, że do obliczeń użyta zostanie najbardziej aktualna wersja stanu (patrz niżej). |
55 | 55 |
|
56 |
| -### What is the difference between passing an object or a function in `setState`? {#what-is-the-difference-between-passing-an-object-or-a-function-in-setstate} |
| 56 | +### Czym różni się przekazanie do `setState` obiektu od przekazania funkcji? {#what-is-the-difference-between-passing-an-object-or-a-function-in-setstate} |
57 | 57 |
|
58 |
| -Passing an update function allows you to access the current state value inside the updater. Since `setState` calls are batched, this lets you chain updates and ensure they build on top of each other instead of conflicting: |
| 58 | +Przekazana funkcja aktualizująca ma dostęp do aktualnej wersji stanu. Jako że wywołania `setState` są grupowane, ten sposób pozwoli ci na stworzenie sekwencji aktualizacji, która zamiast powodować konflikty, będzie operowała na kolejnych aktualnych wersjach stanu: |
59 | 59 |
|
60 | 60 | ```jsx
|
61 | 61 | incrementCount() {
|
62 | 62 | this.setState((state) => {
|
63 |
| - // Important: read `state` instead of `this.state` when updating. |
| 63 | + // Ważne: zamiast z `this.state` odczytuj wartość z argumentu `state`. |
64 | 64 | return {count: state.count + 1}
|
65 | 65 | });
|
66 | 66 | }
|
67 | 67 |
|
68 | 68 | handleSomething() {
|
69 |
| - // Let's say `this.state.count` starts at 0. |
| 69 | + // Załóżmy, że `this.state.count` startuje z wartością 0. |
70 | 70 | this.incrementCount();
|
71 | 71 | this.incrementCount();
|
72 | 72 | this.incrementCount();
|
73 | 73 |
|
74 |
| - // If you read `this.state.count` now, it would still be 0. |
75 |
| - // But when React re-renders the component, it will be 3. |
| 74 | + // Jeśli sprawdzisz teraz wartość `this.state.count`, nadal będzie równa 0. |
| 75 | + // Jednak gdy React ponownie wyrenderuje komponent, zmieni się ona na 3. |
76 | 76 | }
|
77 | 77 | ```
|
78 | 78 |
|
79 |
| -[Learn more about setState](/docs/react-component.html#setstate) |
| 79 | +[Dowiedz się więcej na temat funkcji `setState`.](/docs/react-component.html#setstate) |
80 | 80 |
|
81 |
| -### When is `setState` asynchronous? {#when-is-setstate-asynchronous} |
| 81 | +### Kiedy `setState` działa asynchronicznie? {#when-is-setstate-asynchronous} |
82 | 82 |
|
83 |
| -Currently, `setState` is asynchronous inside event handlers. |
| 83 | +Obecnie `setState` działa asynchronicznie wewnątrz procedur obsługi zdarzeń. |
84 | 84 |
|
85 |
| -This ensures, for example, that if both `Parent` and `Child` call `setState` during a click event, `Child` isn't re-rendered twice. Instead, React "flushes" the state updates at the end of the browser event. This results in significant performance improvements in larger apps. |
| 85 | +Dzięki temu, na przykład, jeśli zarówno komponent-rodzic, jak i komponent-dziecko wywołają `setState` podczas zdarzenia kliknięcia, komponent-dziecko nie zostanie ponownie wyrenderowany dwukrotnie. Zamiast tego React uruchomi wszystkie te aktualizacje stanu na koniec przeglądarkowego zdarzenia. W większych aplikacjach korzystnie wpływa to na wydajność. |
86 | 86 |
|
87 |
| -This is an implementation detail so avoid relying on it directly. In the future versions, React will batch updates by default in more cases. |
| 87 | +Jest to szczegół implementacyjny i staraj się nie polegać na nim bezpośrednio. W przyszłych wersjach React będzie domyślnie grupował aktualizacje w większej liczbie przypadków. |
88 | 88 |
|
89 |
| -### Why doesn't React update `this.state` synchronously? {#why-doesnt-react-update-thisstate-synchronously} |
| 89 | +### Dlaczego React nie aktualizuje `this.state` synchronicznie? {#why-doesnt-react-update-thisstate-synchronously} |
90 | 90 |
|
91 |
| -As explained in the previous section, React intentionally "waits" until all components call `setState()` in their event handlers before starting to re-render. This boosts performance by avoiding unnecessary re-renders. |
| 91 | +Jak wyjaśniliśmy w poprzedniej sekcji, React celowo "czeka", aż wszystkie komponenty wywołają `setState()` w swoich procedurach obsługi zdarzeń, zanim zacznie ponownie renderować drzewo komponentów. Dzięki temu unikamy niepotrzebnych ponownych renderowań, co korzystnie wpływa na wydajność aplikacji. |
92 | 92 |
|
93 |
| -However, you might still be wondering why React doesn't just update `this.state` immediately without re-rendering. |
| 93 | +Mimo wszystko może dziwić cię, dlaczego React tak po prostu nie aktualizuje `this.state` natychmiastowo, bez ponownego renderowania. |
94 | 94 |
|
95 |
| -There are two main reasons: |
| 95 | +Są ku temu dwa powody: |
96 | 96 |
|
97 |
| -* This would break the consistency between `props` and `state`, causing issues that are very hard to debug. |
98 |
| -* This would make some of the new features we're working on impossible to implement. |
| 97 | +* Skutkowałoby to przerwaniem spójności między właściwościami a stanem, powodując bardzo trudne w zlokalizowaniu błędy. |
| 98 | +* Uniemożliwiłoby to nam zaimplementowanie opracowywanych przez nas nowych funkcjonalności. |
99 | 99 |
|
100 |
| -This [GitHub comment](https://github.com/facebook/react/issues/11527#issuecomment-360199710) dives deep into the specific examples. |
| 100 | +Ten [komentarz na GitHubie](https://github.com/facebook/react/issues/11527#issuecomment-360199710) dużo bardziej zagłębia się w konkretne przykłady. |
101 | 101 |
|
102 |
| -### Should I use a state management library like Redux or MobX? {#should-i-use-a-state-management-library-like-redux-or-mobx} |
| 102 | +### Czy powinno się używać bibliotek zarządzających stanem, jak Redux czy MobX? {#should-i-use-a-state-management-library-like-redux-or-mobx} |
103 | 103 |
|
104 |
| -[Maybe.](https://redux.js.org/faq/general#when-should-i-use-redux) |
| 104 | +[Być może.](https://redux.js.org/faq/general#when-should-i-use-redux) |
105 | 105 |
|
106 |
| -It's a good idea to get to know React first, before adding in additional libraries. You can build quite complex applications using only React. |
| 106 | +Dobrze jest jednak najpierw dobrze poznać Reacta, zanim zacznie się dodawać kolejne biblioteki do zestawu. W samym tylko Reakcie można napisać dość złożone aplikacje. |
0 commit comments