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/hooks-custom.md
+39-38
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,16 @@
1
1
---
2
2
id: hooks-custom
3
-
title: Building Your Own Hooks
3
+
title: Saját Horgok készítése
4
4
permalink: docs/hooks-custom.html
5
5
next: hooks-reference.html
6
6
prev: hooks-rules.html
7
7
---
8
8
9
-
*Hooks* are a new addition in React 16.8. They let you use state and other React features without writing a class.
9
+
A *Horgok* a React 16.8-as verziójában lettek hozzáadva. Osztályok létrehozása nélkül is lehetőséget kínálnak állapot, és más React funkciók használatához.
10
10
11
-
Building your own Hooks lets you extract component logic into reusable functions.
11
+
A saját Horgok készítésével lehetőséged van komponenslogika újrafelhasználható függvényekbe való kivonására.
12
12
13
-
When we were learning about [using the Effect Hook](/docs/hooks-effect.html#example-using-hooks-1), we saw this component from a chat application that displays a message indicating whether a friend is online or offline:
13
+
Amikor [a Hatás Horog használatáról](/docs/hooks-effect.html#example-using-hooks-1) tanultunk, láttuk, hogy ez a csevegőalkalmazás komponens egy üzenetet jelenít meg arról, hogy egy barátunk státusza éppen online vagy offline:
14
14
15
15
```js{4-15}
16
16
import React, { useState, useEffect } from 'react';
@@ -30,13 +30,13 @@ function FriendStatus(props) {
30
30
});
31
31
32
32
if (isOnline === null) {
33
-
return 'Loading...';
33
+
return 'Betöltés...';
34
34
}
35
35
return isOnline ? 'Online' : 'Offline';
36
36
}
37
37
```
38
38
39
-
Now let's say that our chat application also has a contact list, and we want to render names of online users with a green color. We could copy and paste similar logic above into our `FriendListItem`component but it wouldn't be ideal:
39
+
Most tegyük fel, hogy a csevegőalkalmazásunk rendelkezik egy kontaktlistával is, és az online lévő felhasználók neveit zöld színnel akarjuk renderelni. Átmásolhatjuk a hasonló logikát a `FriendListItem`komponensből, de ez nem lenne ideális:
40
40
41
41
```js{4-15}
42
42
import React, { useState, useEffect } from 'react';
@@ -63,15 +63,15 @@ function FriendListItem(props) {
63
63
}
64
64
```
65
65
66
-
Instead, we'd like to share this logic between`FriendStatus`and`FriendListItem`.
66
+
Ehelyett jó lenne megosztani ezt a logikát a`FriendStatus`és`FriendListItem` komponensek között.
67
67
68
-
Traditionally in React, we've had two popular ways to share stateful logic between components: [render props](/docs/render-props.html)and [higher-order components](/docs/higher-order-components.html). We will now look at how Hooks solve many of the same problems without forcing you to add more components to the tree.
68
+
Hagyományosan a Reactben két népszerű módja létezett állapot teljes logika komponensek közti megosztására: [render propok](/docs/render-props.html)és [felsőbb rendű komponensek](/docs/higher-order-components.html). Most azt nézzük meg, hogy a Horgok hogyan oldják meg majdnem ugyanazokat a problémákat anélkül, hogy arra kényszerítenének bennünket, hogy új komponenseket adjunk hozzá a fához.
69
69
70
-
## Extracting a Custom Hook {#extracting-a-custom-hook}
70
+
## Egy egyedi Horog kivonása {#extracting-a-custom-hook}
71
71
72
-
When we want to share logic between two JavaScript functions, we extract it to a third function. Both components and Hooks are functions, so this works for them too!
72
+
Amikor logikát szeretnénk megosztani két JavaScript függvény között, azt ki szoktuk vonni egy harmadik függvénybe. Mindkét komponens és a Horgok is függvények, szóval ez itt is működni fog!
73
73
74
-
**A custom Hook is a JavaScript function whose name starts with "`use`" and that may call other Hooks.**For example, `useFriendStatus` below is our first custom Hook:
74
+
**Egy egyedi Horog egy JavaScript függvény, aminek a neve a "`use`" előtaggal kezdődik, és meghívhat más Horgokat is.**Például az alábbi `useFrientStatus` az első egyedi Horgunk:
75
75
76
76
```js{3}
77
77
import { useState, useEffect } from 'react';
@@ -94,11 +94,11 @@ function useFriendStatus(friendID) {
94
94
}
95
95
```
96
96
97
-
There's nothing new inside of it -- the logic is copied from the components above. Just like in a component, make sure to only call other Hooks unconditionally at the top level of your custom Hook.
97
+
Semmi új nincs benne -- a logika a fentebbi komponensből lett átmásolva. Ahogyan egy komponensben is, itt szintén ügyelj rá, hogy a Horgokat ne feltételesen hívd meg az egyedi Horog legfelsőbb szintjén.
98
98
99
-
Unlike a React component, a custom Hook doesn't need to have a specific signature. We can decide what it takes as arguments, and what, if anything, it should return. In other words, it's just like a normal function. Its name should always start with `use` so that you can tell at a glance that the [rules of Hooks](/docs/hooks-rules.html) apply to it.
99
+
Egy React komponenssel ellentétben, egy egyedi Horognak nem kell egy specifikus szignatúrával rendelkeznie. Mi dönthetünk az argumentumokról, és hogy mit adjon vissza, ha egyáltalán bármit is vissza kell adnia. Más szóval ez csak egy egyszerű függvény. A neve mindig `use`-val kell hogy kezdődjön annak érdekében, hogy első pillantásra el tudd dönteni, hogy vonatkoznak-e rá a [Horgok szabályai](/docs/hooks-rules.html).
100
100
101
-
The purpose of our `useFriendStatus`Hook is to subscribe us to a friend's status. This is why it takes `friendID` as an argument, and returns whether this friend is online:
101
+
A `useFriendStatus`Horgunk lényege, hogy feliratkozzon egy barátunk státuszára. Ezért fogad egy `friendID`-t argumentumként, és adja vissza ennek a barátnak az online státuszát:
102
102
103
103
```js
104
104
functionuseFriendStatus(friendID) {
@@ -110,20 +110,21 @@ function useFriendStatus(friendID) {
110
110
}
111
111
```
112
112
113
-
Now let's see how we can use our custom Hook.
113
+
Most lássuk, hogy hogyan tudjuk használni az egyedi Horgunkat.
114
114
115
-
## Using a Custom Hook {#using-a-custom-hook}
115
+
## Egyedi Horog használata {#using-a-custom-hook}
116
116
117
-
In the beginning, our stated goal was to remove the duplicated logic from the `FriendStatus` and `FriendListItem` components. Both of them want to know whether a friend is online.
117
+
A kezdetben a kitűzött célunk az volt, hogy eltávolítsuk a duplikált logikát a `FriendStatus` és `FriendListitem`
118
+
komponensekből. Mindkettő a barátunk státuszát szeretné tudni.
118
119
119
-
Now that we've extracted this logic to a `useFriendStatus`hook, we can *just use it:*
120
+
Most hogy kivontuk ezt a logikát egy `useFriendStatus`horogba, *egyszerűen elkezdhetjük ezt használni*:
@@ -141,19 +142,19 @@ function FriendListItem(props) {
141
142
}
142
143
```
143
144
144
-
**Is this code equivalent to the original examples?**Yes, it works in exactly the same way. If you look closely, you'll notice we didn't make any changes to the behavior. All we did was to extract some common code between two functions into a separate function. **Custom Hooks are a convention that naturally follows from the design of Hooks, rather than a React feature.**
145
+
**Ez a kód megegyezik az eredeti példával?**Igen, pontosan ugyanúgy működik. Ha közelebbről is megnézed, észreveheted, hogy semmilyen változást nem eszközöltünk a viselkedésen. Csak annyit tettünk, hogy kivontunk valamennyi közös kódot két függvényből egy különálló függvénybe. **Az egyedi Horgok egy olyan konvenció, ami természetesen következik a Horgok tervezéséből, inkább mint hogy egy React funkció lenne.**
145
146
146
-
**Do I have to name my custom Hooks starting with “`use`”?**Please do. This convention is very important. Without it, we wouldn't be able to automatically check for violations of [rules of Hooks](/docs/hooks-rules.html)because we couldn't tell if a certain function contains calls to Hooks inside of it.
147
+
**Muszáj az egyedi Horgaim neveit "`use`"-val kezdenem?**Kérünk, tedd. Ez a konvenció nagyon fontos. Enélkül nem tudjuk automatikusan leellenőrizni, hogy a [Horgok szabályai](/docs/hooks-rules.html)meg lettek-e szegve, mert nem tudhatjuk, hogy egy bizonyos függvényen belül vannak-e Horog meghívások.
147
148
148
-
**Do two components using the same Hook share state?**No. Custom Hooks are a mechanism to reuse *stateful logic* (such as setting up a subscription and remembering the current value), but every time you use a custom Hook, all state and effects inside of it are fully isolated.
149
+
**Két, ugyanazt a Horgot használó komponens saját állapotukat is megosztja?**Nem. Az egyedi Horgok egy mechanizmus *állapot teljes logika* újrafelhasználására (mint például feliratkozás, vagy jelenlegi érték megjegyzése), de minden alkalommal amikor egy egyedi Horgot használsz, minden állapot és hatás teljesen elzártan működik.
149
150
150
-
**How does a custom Hook get isolated state?**Each *call* to a Hook gets isolated state. Because we call `useFriendStatus` directly, from React's point of view our component just calls `useState`and`useEffect`. And as we [learned](/docs/hooks-state.html#tip-using-multiple-state-variables)[earlier](/docs/hooks-effect.html#tip-use-multiple-effects-to-separate-concerns), we can call `useState`and`useEffect`many times in one component, and they will be completely independent.
151
+
**Hogyan kap egy egyedi Horog elzárt állapotot?**A Horog minden *meghívása* elzárt állapottal rendelkezik. Mivel a `useFriendStatus`-t közvetlenül hívjuk meg, a React szemszögéből a komponensünk csak `useState`és`useEffect` hívásokat végez. És ahogy [korábban](/docs/hooks-effect.html#tip-use-multiple-effects-to-separate-concerns)[megtanultuk](/docs/hooks-state.html#tip-using-multiple-state-variables), a `useState`és`useEffect`akárhányszor meghívható egy komponensben, és azok teljesen függetlenek lesznek.
151
152
152
-
### Tip: Pass Information Between Hooks {#tip-pass-information-between-hooks}
153
+
### Tipp: Információ átadása Horgok között {#tip-pass-information-between-hooks}
153
154
154
-
Since Hooks are functions, we can pass information between them.
155
+
Mivel a Horgok függvények, át tudunk adni információt közöttük.
155
156
156
-
To illustrate this, we'll use another component from our hypothetical chat example. This is a chat message recipient picker that displays whether the currently selected friend is online:
157
+
Hogy ezt illusztráljuk, egy másik komponenst használunk az elméleti csevegőalkalmazás példánkból. Ez egy csevegő üzenet címzettválasztó, ami azt jelzi, hogy a jelenleg választott barátunk online van-e.
157
158
158
159
```js{8-9,13}
159
160
const friendList = [
@@ -184,24 +185,24 @@ function ChatRecipientPicker() {
184
185
}
185
186
```
186
187
187
-
We keep the currently chosen friend ID in the `recipientID`state variable, and update it if the user chooses a different friend in the `<select>`picker.
188
+
A választott barát azonosítóját a `recipientID`állapot változóban tartjuk, és frissítjük, ha a felhasználó egy másik barátot választ a `<select>`menüből.
188
189
189
-
Because the`useState`Hook call gives us the latest value of the `recipientID`state variable, we can pass it to our custom`useFriendStatus`Hook as an argument:
190
+
Mivel a`useState`Horog meghívás a a `recipientID`állapot változó legújabb értékét adja vissza, ezt átadhatjuk a`useFriendStatus`Horognak argumentumként:
This lets us know whether the *currently selected* friend is online. If we pick a different friend and update the `recipientID`state variable, our`useFriendStatus`Hook will unsubscribe from the previously selected friend, and subscribe to the status of the newly selected one.
197
+
Így tudjuk, ha a *jelenleg kiválasztott* barátunk online van-e. Ha egy másik barátot választunk, és frissítjük a `recipientID`állapot változót, a`useFriendStatus`Horgunk leiratkozik az előzőleg választott barátunkról, és feliratkozik az újonnan választott státuszára.
197
198
198
-
## `useYourImagination()` {#useyourimagination}
199
+
## `useYourImagination()`(Használd a képzeleted) {#useyourimagination}
199
200
200
-
Custom Hooks offer the flexibility of sharing logic that wasn't possible in React components before. You can write custom Hooks that cover a wide range of use cases like form handling, animation, declarative subscriptions, timers, and probably many more we haven't considered. What's more, you can build Hooks that are just as easy to use as React's built-in features.
201
+
Az egyedi Horgok olyan flexibilitást nyújtanak logika megosztására, ami korábban nem volt lehetséges a React komponensekben. Írhatsz egy egyedi Horgot ami űrlapkezeléstől animáción, deklaratív feliratkozásokon, és időzítőkön át, valószínűleg általunk nem is gondolt eseteket is lefed. Mi több, olyan Horgokat építhetsz, amiket ugyanolyan könnyű használni, mint a React beépített funkcióit.
201
202
202
-
Try to resist adding abstraction too early. Now that function components can do more, it's likely that the average function component in your codebase will become longer. This is normal -- don't feel like you *have to* immediately split it into Hooks. But we also encourage you to start spotting cases where a custom Hook could hide complex logic behind a simple interface, or help untangle a messy component.
203
+
Próbálj a korai absztrakcióknak ellenállni. Most, hogy a függvénykomponensek többet tudnak csinálni, valószínű, hogy, az átlag függvénykomponensed a kódbázisodban hosszabb lesz. Ez normális -- ne érezd úgy, hogy *muszáj* azonnal Horgokra lebontanod azt. De arra is bátorítunk, hogy próbálj meg olyan esetek után fürkészni, ahol egy komplex logikát egy egyszerű interfész mögé tudnál rejteni, vagy egy zavaros komponenst tudsz átláthatóbbá tenni egyedi Horgokkal.
203
204
204
-
For example, maybe you have a complex component that contains a lot of local state that is managed in an ad-hoc way. `useState`doesn't make centralizing the update logic any easier so you might prefer to write it as a [Redux](https://redux.js.org/) reducer:
205
+
Lehet például, hogy van egy komplex komponensed, ami sok helyi állapotot tartalmaz, amit alkalmi módon kezelsz. A `useState`nem teszi egyszerűbbé a frissítési logika centralizálását, szóval lehet, hogy egy [Redux](https://redux.js.org/)-szerű redukátort inkább preferálnál:
205
206
206
207
```js
207
208
functiontodosReducer(state, action) {
@@ -211,16 +212,16 @@ function todosReducer(state, action) {
211
212
text:action.text,
212
213
completed:false
213
214
}];
214
-
// ... other actions ...
215
+
// ... más akciók ...
215
216
default:
216
217
return state;
217
218
}
218
219
}
219
220
```
220
221
221
-
Reducers are very convenient to test in isolation, and scale to express complex update logic. You can further break them apart into smaller reducers if necessary. However, you might also enjoy the benefits of using React local state, or might not want to install another library.
222
+
A redukátorokat nagyon kézenfekvő elzártan tesztelni, és jól skálázódnak komplex frissítési logika kifejezése esetén. Továbbá kisebb redukátorokra is lebonthatod őket, ha szükséges. Azonban lehet, hogy szimplán élvezed a React helyi állapotának előnyeit, vagy csak nem akarsz egy extra könyvtárat telepíteni.
222
223
223
-
So what if we could write a`useReducer`Hook that lets us manage the *local* state of our component with a reducer? A simplified version of it might look like this:
224
+
Szóval mi lenne, ha írni tudnánk egy`useReducer`Horgot, ami a komponensünk *helyi* állapotát tudná kezelni egy redukátorral? Egy leegyszerűsített változat így nézne ki:
224
225
225
226
```js
226
227
functionuseReducer(reducer, initialState) {
@@ -235,7 +236,7 @@ function useReducer(reducer, initialState) {
235
236
}
236
237
```
237
238
238
-
Now we could use it in our component, and let the reducer drive its state management:
239
+
Ezt így tudnánk használni a komponensünkben, ahol a redukátor hajtaná az állapotkezelést:
239
240
240
241
```js{2}
241
242
function Todos() {
@@ -249,4 +250,4 @@ function Todos() {
249
250
}
250
251
```
251
252
252
-
The need to manage local state with a reducer in a complex component is common enough that we've built the `useReducer`Hook right into React. You'll find it together with other built-in Hooks in the [Hooks API reference](/docs/hooks-reference.html).
253
+
Az igény arra, hogy komplex komponensek helyi állapotát kezelni tudjuk egy redukátorral elég általános, így a `useReducer`Horgot közvetlenül beépítettük a Reactbe. Megtalálható a többi beépített Horoggal együtt, a [Horgok API referencia](/docs/hooks-reference.html) oldalon.
0 commit comments