Skip to content

Commit 02087f2

Browse files
committed
🚧 🌐 WIP translate Lifting State Up
1 parent 70bb69a commit 02087f2

File tree

1 file changed

+50
-50
lines changed

1 file changed

+50
-50
lines changed

content/docs/lifting-state-up.md

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: lifting-state-up
3-
title: Lifting State Up
3+
title: Állapot felemelése
44
permalink: docs/lifting-state-up.html
55
prev: forms.html
66
next: composition-vs-inheritance.html
@@ -9,24 +9,24 @@ redirect_from:
99
- "docs/flux-todo-list.html"
1010
---
1111

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.
1313

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.
1515

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:
1717

1818
```js{3,5}
1919
function BoilingVerdict(props) {
2020
if (props.celsius >= 100) {
21-
return <p>The water would boil.</p>;
21+
return <p>A víz forrna.</p>;
2222
}
23-
return <p>The water would not boil.</p>;
23+
return <p>A víz nem forrna.</p>;
2424
}
2525
```
2626

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.
2828

29-
Additionally, it renders the `BoilingVerdict` for the current input value.
29+
Továbbá rendereli a `BoilingVerdict`-et is a jelenlegi bevitt értékkel.
3030

3131
```js{5,9,13,17-21}
3232
class Calculator extends React.Component {
@@ -44,7 +44,7 @@ class Calculator extends React.Component {
4444
const temperature = this.state.temperature;
4545
return (
4646
<fieldset>
47-
<legend>Enter temperature in Celsius:</legend>
47+
<legend>Adja meg a hőmérsékletet Celsius egységben:</legend>
4848
<input
4949
value={temperature}
5050
onChange={this.handleChange} />
@@ -56,13 +56,13 @@ class Calculator extends React.Component {
5656
}
5757
```
5858

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)
6060

61-
## Adding a Second Input {#adding-a-second-input}
61+
## Második input hozzáadása {#adding-a-second-input}
6262

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.
6464

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"`:
6666

6767
```js{1-4,19,22}
6868
const scaleNames = {
@@ -86,7 +86,7 @@ class TemperatureInput extends React.Component {
8686
const scale = this.props.scale;
8787
return (
8888
<fieldset>
89-
<legend>Enter temperature in {scaleNames[scale]}:</legend>
89+
<legend>Adja meg a hőmérsékletet {scaleNames[scale]} egységben:</legend>
9090
<input value={temperature}
9191
onChange={this.handleChange} />
9292
</fieldset>
@@ -95,7 +95,7 @@ class TemperatureInput extends React.Component {
9595
}
9696
```
9797

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:
9999

100100
```js{5,6}
101101
class Calculator extends React.Component {
@@ -110,15 +110,15 @@ class Calculator extends React.Component {
110110
}
111111
```
112112

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)
114114

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.
116116

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.
118118

119-
## Writing Conversion Functions {#writing-conversion-functions}
119+
## Konvertáló függvények írása {#writing-conversion-functions}
120120

121-
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:
122122

123123
```js
124124
function toCelsius(fahrenheit) {
@@ -130,9 +130,9 @@ function toFahrenheit(celsius) {
130130
}
131131
```
132132

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.
134134

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:
136136

137137
```js
138138
function tryConvert(temperature, convert) {
@@ -146,11 +146,11 @@ function tryConvert(temperature, convert) {
146146
}
147147
```
148148

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.
150150

151-
## Lifting State Up {#lifting-state-up}
151+
## Állapot felemelése {#lifting-state-up}
152152

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:
154154

155155
```js{5,9,13}
156156
class TemperatureInput extends React.Component {
@@ -169,43 +169,43 @@ class TemperatureInput extends React.Component {
169169
// ...
170170
```
171171

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.
173173

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.
175175

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.
177177

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.
179179

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:
181181

182182
```js{3}
183183
render() {
184-
// Before: const temperature = this.state.temperature;
184+
// Előtte: const temperature = this.state.temperature;
185185
const temperature = this.props.temperature;
186186
// ...
187187
```
188188

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.
190190

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.
192192

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:
194194

195195
```js{3}
196196
handleChange(e) {
197-
// Before: this.setState({temperature: e.target.value});
197+
// Előtte: this.setState({temperature: e.target.value});
198198
this.props.onTemperatureChange(e.target.value);
199199
// ...
200200
```
201201

202-
>Note:
202+
>Megjegyzés:
203203
>
204-
>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.
205205
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.
207207

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:
209209

210210
```js{8,12}
211211
class TemperatureInput extends React.Component {
@@ -223,7 +223,7 @@ class TemperatureInput extends React.Component {
223223
const scale = this.props.scale;
224224
return (
225225
<fieldset>
226-
<legend>Enter temperature in {scaleNames[scale]}:</legend>
226+
<legend>Adja meg a hőmérsékletet {scaleNames[scale]} egységben:</legend>
227227
<input value={temperature}
228228
onChange={this.handleChange} />
229229
</fieldset>
@@ -232,11 +232,11 @@ class TemperatureInput extends React.Component {
232232
}
233233
```
234234

235-
Now let's turn to the `Calculator` component.
235+
Most pedig forduljunk a `Calculator` komopnens felé.
236236

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.
238238

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:
240240

241241
```js
242242
{
@@ -245,7 +245,7 @@ For example, if we enter 37 into the Celsius input, the state of the `Calculator
245245
}
246246
```
247247

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:
249249

250250
```js
251251
{
@@ -254,9 +254,9 @@ If we later edit the Fahrenheit field to be 212, the state of the `Calculator` w
254254
}
255255
```
256256

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.
258258

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:
260260

261261
```js{6,10,14,18-21,27-28,31-32,34}
262262
class Calculator extends React.Component {
@@ -299,11 +299,11 @@ class Calculator extends React.Component {
299299
}
300300
```
301301

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)
303303

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.
305305

306-
Let's recap what happens when you edit an input:
306+
Vegyük át mi történik mikor egy inputot szerkesztesz:
307307

308308
* React calls the function specified as `onChange` on the DOM `<input>`. In our case, this is the `handleChange` method in the `TemperatureInput` component.
309309
* 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:
316316

317317
Every update goes through the same steps so the inputs stay in sync.
318318

319-
## Lessons Learned {#lessons-learned}
319+
## Megtanult leckék {#lessons-learned}
320320

321321
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).
322322

0 commit comments

Comments
 (0)