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/lifting-state-up.md
+50-50Lines changed: 50 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
id: lifting-state-up
3
-
title: Lifting State Up
3
+
title: Állapot felemelése
4
4
permalink: docs/lifting-state-up.html
5
5
prev: forms.html
6
6
next: composition-vs-inheritance.html
@@ -9,24 +9,24 @@ redirect_from:
9
9
- "docs/flux-todo-list.html"
10
10
---
11
11
12
-
Often, several components need to reflect the same changing data. We recommend lifting the shared state up to their closest common ancestor. Let's see how this works in action.
12
+
Gyakran több komponens kell hogy ugyanzon adat változását tükrözze. Ebben az esetben a megosztott állapot legközelebbi közös ősbe való felemelését ajánljuk. Lássuk hogy hogyan is működik ez a gyakorlatban.
13
13
14
-
In this section, we will create a temperature calculator that calculates whether the water would boil at a given temperature.
14
+
Ebben a fejezetben egy hőmérséklet kalkulátort fogunk készíteni ami azt számolja ki, hogy a víz forr-e egy adott hőmérsékleten.
15
15
16
-
We will start with a component called `BoilingVerdict`. It accepts the `celsius`temperature as a prop, and prints whether it is enough to boil the water:
16
+
Kezdjük egy `BoilingVerdict` komponenssel. Ez egy `celsius`prop-ot fogad, és kiírja, hogy ez elég-e a víz forrásához:
17
17
18
18
```js{3,5}
19
19
function BoilingVerdict(props) {
20
20
if (props.celsius >= 100) {
21
-
return <p>The water would boil.</p>;
21
+
return <p>A víz forrna.</p>;
22
22
}
23
-
return <p>The water would not boil.</p>;
23
+
return <p>A víz nem forrna.</p>;
24
24
}
25
25
```
26
26
27
-
Next, we will create a component called `Calculator`. It renders an `<input>` that lets you enter the temperature, and keeps its value in`this.state.temperature`.
27
+
A következőben egy `Calculator` komponenst készítünk. Ez egy `<input>`-ot renderel ami lehetővé teszi a hőmérséklet bevitelét és annak értékét a`this.state.temperature`-ban tárolja.
28
28
29
-
Additionally, it renders the `BoilingVerdict` for the current input value.
29
+
Továbbá rendereli a `BoilingVerdict`-et is a jelenlegi bevitt értékkel.
30
30
31
31
```js{5,9,13,17-21}
32
32
class Calculator extends React.Component {
@@ -44,7 +44,7 @@ class Calculator extends React.Component {
44
44
const temperature = this.state.temperature;
45
45
return (
46
46
<fieldset>
47
-
<legend>Enter temperature in Celsius:</legend>
47
+
<legend>Adja meg a hőmérsékletet Celsius egységben:</legend>
48
48
<input
49
49
value={temperature}
50
50
onChange={this.handleChange} />
@@ -56,13 +56,13 @@ class Calculator extends React.Component {
56
56
}
57
57
```
58
58
59
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/ZXeOBm?editors=0010)
59
+
[**Próbáld ki CodePen-en**](https://codepen.io/gaearon/pen/ZXeOBm?editors=0010)
60
60
61
-
## Adding a Second Input {#adding-a-second-input}
61
+
## Második input hozzáadása {#adding-a-second-input}
62
62
63
-
Our new requirement is that, in addition to a Celsius input, we provide a Fahrenheit input, and they are kept in sync.
63
+
A legújabb követelményünk a Celsius input mellett egy Fahrenheit beviteli mező, és hogy ezek szinkronban legyenek.
64
64
65
-
We can start by extracting a `TemperatureInput`component from `Calculator`. We will add a new `scale` prop to it that can either be `"c"`or`"f"`:
65
+
Kezdhetjük egy `TemperatureInput`komponens kivonásával a `Calculator`-ból. Hozzáadunk egy `scale` prop-ot ami lehet `"c"`vagy`"f"`:
66
66
67
67
```js{1-4,19,22}
68
68
const scaleNames = {
@@ -86,7 +86,7 @@ class TemperatureInput extends React.Component {
86
86
const scale = this.props.scale;
87
87
return (
88
88
<fieldset>
89
-
<legend>Enter temperature in {scaleNames[scale]}:</legend>
89
+
<legend>Adja meg a hőmérsékletet {scaleNames[scale]} egységben:</legend>
90
90
<input value={temperature}
91
91
onChange={this.handleChange} />
92
92
</fieldset>
@@ -95,7 +95,7 @@ class TemperatureInput extends React.Component {
95
95
}
96
96
```
97
97
98
-
We can now change the `Calculator`to render two separate temperature inputs:
98
+
Így most meg tudjuk változtatni a `Calculator`komponenst, hogy az két külön hőmérséklet inputot rendereljen:
99
99
100
100
```js{5,6}
101
101
class Calculator extends React.Component {
@@ -110,15 +110,15 @@ class Calculator extends React.Component {
110
110
}
111
111
```
112
112
113
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/jGBryx?editors=0010)
113
+
[**Próbáld ki CodePen-en**](https://codepen.io/gaearon/pen/jGBryx?editors=0010)
114
114
115
-
We have two inputs now, but when you enter the temperature in one of them, the other doesn't update. This contradicts our requirement: we want to keep them in sync.
115
+
Most már két beivteli mezőnk van, de ha az egyikbe beírod a hőmérsékletet, a másik nem frissül. Ez ellentmond a követelményünknek: az értékek legyenek szinkronban.
116
116
117
-
We also can't display the `BoilingVerdict` from `Calculator`. The`Calculator`doesn't know the current temperature because it is hidden inside the`TemperatureInput`.
117
+
Valamint a `BoilingVerdict`-et sem tudjuk megjeleníteni a `Calculator`-ból. A`Calculator`nem ismeri a jelenlegi hőmérsékletet mert az el van rejtve a`TemperatureInput`-ban.
First, we will write two functions to convert from Celsius to Fahrenheit and back:
121
+
Először írjunk két függvényt ami a Celsius-t konvertálja Fahrenheit-té és vissza:
122
122
123
123
```js
124
124
functiontoCelsius(fahrenheit) {
@@ -130,9 +130,9 @@ function toFahrenheit(celsius) {
130
130
}
131
131
```
132
132
133
-
These two functions convert numbers. We will write another function that takes a string `temperature`and a converter function as arguments and returns a string. We will use it to calculate the value of one input based on the other input.
133
+
Ez a két függvény számokat konvertál. Írunk egy harmadik függvényt, ami vesz egy `temperature`sztringet és egy konvertáló függvényt argumentumként, és egy sztringet ad vissza. Ezt arra fogjuk használni, hogy az egyik input értékét kiszámoljuk a ámsik értékének alapján.
134
134
135
-
It returns an empty string on an invalid `temperature`, and it keeps the output rounded to the third decimal place:
135
+
Érvénytelen `temperature` esetében egy üres sztringet ad vissza, és az értéket három tizedesjegyre kerekíti:
136
136
137
137
```js
138
138
functiontryConvert(temperature, convert) {
@@ -146,11 +146,11 @@ function tryConvert(temperature, convert) {
146
146
}
147
147
```
148
148
149
-
For example,`tryConvert('abc', toCelsius)`returns an empty string, and `tryConvert('10.22', toFahrenheit)`returns`'50.396'`.
149
+
Például a`tryConvert('abc', toCelsius)`egy üres sztringet ad vissza, és a `tryConvert('10.22', toFahrenheit)`pedig`'50.396'`-t.
150
150
151
-
## Lifting State Up {#lifting-state-up}
151
+
## Állapot felemelése {#lifting-state-up}
152
152
153
-
Currently, both`TemperatureInput`components independently keep their values in the local state:
153
+
Jelenleg mindkét`TemperatureInput`komponens függetlenül tárolja a saját helyi állapotát:
154
154
155
155
```js{5,9,13}
156
156
class TemperatureInput extends React.Component {
@@ -169,43 +169,43 @@ class TemperatureInput extends React.Component {
169
169
// ...
170
170
```
171
171
172
-
However, we want these two inputs to be in sync with each other. When we update the Celsius input, the Fahrenheit input should reflect the converted temperature, and vice versa.
172
+
Azonban azt szeretnénk, ha a két input szinkronban lenne egymással. Ha frissítjük a Celsius inputot, a Fahrenheit-nek tükröznie kell a konvertált hőmérsékletet, és oda-vissza.
173
173
174
-
In React, sharing state is accomplished by moving it up to the closest common ancestor of the components that need it. This is called "lifting state up". We will remove the local state from the `TemperatureInput` and move it into the `Calculator` instead.
174
+
A React-ben az állapot megosztása azon komponensek között amelyek azt igénylik úgy történik, hogy az állapotot azok legközelebbi közös ősébe mozgatjuk. Ezt hívjuk a "állapot felemelésének". El fogjuk távolítani a helyi állapotot a `TemperatureInput`-ból és a `Calculator`-ba költöztetjük azt.
175
175
176
-
If the`Calculator`owns the shared state, it becomes the "source of truth" for the current temperature in both inputs. It can instruct them both to have values that are consistent with each other. Since the props of both `TemperatureInput`components are coming from the same parent `Calculator`component, the two inputs will always be in sync.
176
+
Ha a`Calculator`birtokolja a megosztott állapotot, ezzel a jelenlegi hőmérséklet "igazságának forrásává" válik mindkét input esetében. Mindkét inputot utasítani tudja, hogy olyan értéket vegyenek fel, ami konzisztens a másikkal. Mivel mindkét `TemperatureInput`komponens propjai ugyanabból a szülő `Calculator`komponensből jönnek, a két input így mindig szinkronban lesz.
177
177
178
-
Let's see how this works step by step.
178
+
Lássuk lépésről-lépésre hogyan is működik ez.
179
179
180
-
First, we will replace`this.state.temperature` with `this.props.temperature` in the `TemperatureInput`component. For now, let's pretend`this.props.temperature`already exists, although we will need to pass it from the `Calculator` in the future:
180
+
Előszőr is, cseréljük le a`this.state.temperature`-t `this.props.temperature`-ra a `TemperatureInput`komponensben. Átmenetileg tegyük fel, hogy a`this.props.temperature`létezik, bár később le kell azt küldjük a `Calculator`-ból:
181
181
182
182
```js{3}
183
183
render() {
184
-
// Before: const temperature = this.state.temperature;
184
+
// Előtte: const temperature = this.state.temperature;
185
185
const temperature = this.props.temperature;
186
186
// ...
187
187
```
188
188
189
-
We know that [props are read-only](/docs/components-and-props.html#props-are-read-only). When the`temperature`was in the local state, the`TemperatureInput`could just call `this.setState()` to change it. However, now that the `temperature`is coming from the parent as a prop, the `TemperatureInput`has no control over it.
189
+
Azt tudjuk, hogy a [prop-ok csak olvashatóak](/docs/components-and-props.html#props-are-read-only). Mikor a`temperature`a helyi állapotban volt, a`TemperatureInput`csak egyszerűen meg tudta hívni a `this.setState()`-t annak megváltoztatásához. Azzal hogy a `temperature`a szülő komponensből most prop-ként jön, a `TemperatureInput`elvesztette az irányítást felette.
190
190
191
-
In React, this is usually solved by making a component "controlled". Just like the DOM `<input>`accepts both a `value`and an`onChange` prop, so can the custom `TemperatureInput`accept both `temperature`and `onTemperatureChange`props from its parent `Calculator`.
191
+
A React-ben ezt általában úgy oldunk meg, hogy egy komponenst "kontrollálttá" teszünk. Ugyanúgy ahogy a DOM `<input>`fogad `value`és egy`onChange` prop-ot, az egyedi `TemperatureInput`is fogadhat egy `temperature`és egy `onTemperatureChange`prop-ot annak szülő komponensétől, a `Calculator`-tól.
192
192
193
-
Now, when the`TemperatureInput`wants to update its temperature, it calls `this.props.onTemperatureChange`:
193
+
Most, amikor a`TemperatureInput`frissíteni akarja annak hőmérsékletét, a `this.props.onTemperatureChange`-t fogja meghívni:
>There is no special meaning to either `temperature`or`onTemperatureChange` prop names in custom components. We could have called them anything else, like name them `value`and`onChange` which is a common convention.
204
+
>A `temperature`és`onTemperatureChange` prop-oknak nincs különösebb jelentésük egyedi komponensekben. Bárminek hívhattuk volna őket, például `value`és`onChange`, ami egy gyakori szokás.
205
205
206
-
The`onTemperatureChange` prop will be provided together with the `temperature` prop by the parent `Calculator`component. It will handle the change by modifying its own local state, thus re-rendering both inputs with the new values. We will look at the new `Calculator`implementation very soon.
206
+
A`onTemperatureChange` prop a `temperature` prop-pal együtt a szülő komponens `Calculator`által lesz szolgáltatva. Ez fogja kezelni a változást annak saját helyi állapotát változtatva, ezzel újrarenderelve mindkét inputot az új értékekkel. Nemsokára megnézzük a `Calculator`új implementációját is.
207
207
208
-
Before diving into the changes in the `Calculator`, let's recap our changes to the `TemperatureInput`component. We have removed the local state from it, and instead of reading `this.state.temperature`, we now read `this.props.temperature`. Instead of calling `this.setState()`when we want to make a change, we now call `this.props.onTemperatureChange()`, which will be provided by the `Calculator`:
208
+
Mielőtt belemerülnénk a `Calculator` változtatásaiba, vegyük át a `TemperatureInput`konmponensen eszközölt változtatásainkat. Eltávolítottuk annak helyi állapotát és a `this.state.temperature` olvasása helyett most a `this.props.temperature`-t olvassuk. A `this.setState()`meghívása helyett ha változást akarunk okozni, most a `this.props.onTemperatureChange()` metódust hívjuk meg, amit a `Calculator` szolgáltat majd:
209
209
210
210
```js{8,12}
211
211
class TemperatureInput extends React.Component {
@@ -223,7 +223,7 @@ class TemperatureInput extends React.Component {
223
223
const scale = this.props.scale;
224
224
return (
225
225
<fieldset>
226
-
<legend>Enter temperature in {scaleNames[scale]}:</legend>
226
+
<legend>Adja meg a hőmérsékletet {scaleNames[scale]} egységben:</legend>
227
227
<input value={temperature}
228
228
onChange={this.handleChange} />
229
229
</fieldset>
@@ -232,11 +232,11 @@ class TemperatureInput extends React.Component {
232
232
}
233
233
```
234
234
235
-
Now let's turn to the `Calculator`component.
235
+
Most pedig forduljunk a `Calculator`komopnens felé.
236
236
237
-
We will store the current input's `temperature`and`scale`in its local state. This is the state we "lifted up" from the inputs, and it will serve as the "source of truth" for both of them. It is the minimal representation of all the data we need to know in order to render both inputs.
237
+
A jelenlegi inputot a `temperature`és`scale`helyi állapotban fogjuk tárolni. Ez az beviteli mezőkből "felemelt" állapot és ez fog az "igazság forrásaként" szolgálni mindkettőnek. Ez az összes adat minimális reprezentációja amit ismernünk kell annak érdekében, hogy mindkét inputot renderelni tudjuk.
238
238
239
-
For example, if we enter 37 into the Celsius input, the state of the `Calculator`component will be:
239
+
Például ha 37-et írunk be a Celsius beviteli mezőbe, a `Calculator`komponens állapota így fog kinézni:
240
240
241
241
```js
242
242
{
@@ -245,7 +245,7 @@ For example, if we enter 37 into the Celsius input, the state of the `Calculator
245
245
}
246
246
```
247
247
248
-
If we later edit the Fahrenheit field to be 212, the state of the `Calculator`will be:
248
+
Ha később a Farhernheit mezőbe írunk 212-t, a `Calculator`állapota így fog kinézni:
249
249
250
250
```js
251
251
{
@@ -254,9 +254,9 @@ If we later edit the Fahrenheit field to be 212, the state of the `Calculator` w
254
254
}
255
255
```
256
256
257
-
We could have stored the value of both inputs but it turns out to be unnecessary. It is enough to store the value of the most recently changed input, and the scale that it represents. We can then infer the value of the other input based on the current `temperature` and `scale` alone.
257
+
Eltárolhattuk volna mindkét input értékét is, de úgy néz ki hogy ez felesleges. Elég ha csak a legutoljára változott inputot tároljuk, és a mértékegységet amit képvisel. Eztuán szimplán a jelenlegi `temperature` és `scale` értékekből ki tudjuk következtetni a másik input értékét.
258
258
259
-
The inputs stay in sync because their values are computed from the same state:
259
+
Az inputok szinkronizálva lesznek, mivel azok értékei ugyanabból az állapotból vannak kalkulálva:
260
260
261
261
```js{6,10,14,18-21,27-28,31-32,34}
262
262
class Calculator extends React.Component {
@@ -299,11 +299,11 @@ class Calculator extends React.Component {
299
299
}
300
300
```
301
301
302
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/WZpxpz?editors=0010)
302
+
[**Próbáld ki CodePen-en**](https://codepen.io/gaearon/pen/WZpxpz?editors=0010)
303
303
304
-
Now, no matter which input you edit, `this.state.temperature`and `this.state.scale`in the `Calculator` get updated. One of the inputs gets the value as is, so any user input is preserved, and the other input value is always recalculated based on it.
304
+
Így ha most bármelyik inputot szerkesztjük, a `this.state.temperature`és a `this.state.scale`frissülni fog a `Calculator`-ban. Az egyik input úgy kapja meg az értéket ahogy az el van tárolva, szóval bármilyen felhasználói input meg van őrizve, míg a másik input mindig ez alapján lesz újrakalkulálva.
305
305
306
-
Let's recap what happens when you edit an input:
306
+
Vegyük át mi történik mikor egy inputot szerkesztesz:
307
307
308
308
* React calls the function specified as `onChange` on the DOM `<input>`. In our case, this is the `handleChange` method in the `TemperatureInput` component.
309
309
* The `handleChange` method in the `TemperatureInput` component calls `this.props.onTemperatureChange()` with the new desired value. Its props, including `onTemperatureChange`, were provided by its parent component, the `Calculator`.
@@ -316,7 +316,7 @@ Let's recap what happens when you edit an input:
316
316
317
317
Every update goes through the same steps so the inputs stay in sync.
318
318
319
-
## Lessons Learned {#lessons-learned}
319
+
## Megtanult leckék {#lessons-learned}
320
320
321
321
There should be a single "source of truth" for any data that changes in a React application. Usually, the state is first added to the component that needs it for rendering. Then, if other components also need it, you can lift it up to their closest common ancestor. Instead of trying to sync the state between different components, you should rely on the [top-down data flow](/docs/state-and-lifecycle.html#the-data-flows-down).
0 commit comments