Skip to content

Commit 7811263

Browse files
authored
Merge pull request #60 from reactjs/hooks/custom-hooks
Building Your Own Hooks translation
2 parents 75ec1eb + 5ed3844 commit 7811263

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

content/docs/hooks-custom.md

+39-38
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
---
22
id: hooks-custom
3-
title: Building Your Own Hooks
3+
title: Saját Horgok készítése
44
permalink: docs/hooks-custom.html
55
next: hooks-reference.html
66
prev: hooks-rules.html
77
---
88

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

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

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

1515
```js{4-15}
1616
import React, { useState, useEffect } from 'react';
@@ -30,13 +30,13 @@ function FriendStatus(props) {
3030
});
3131
3232
if (isOnline === null) {
33-
return 'Loading...';
33+
return 'Betöltés...';
3434
}
3535
return isOnline ? 'Online' : 'Offline';
3636
}
3737
```
3838

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

4141
```js{4-15}
4242
import React, { useState, useEffect } from 'react';
@@ -63,15 +63,15 @@ function FriendListItem(props) {
6363
}
6464
```
6565

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

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

70-
## Extracting a Custom Hook {#extracting-a-custom-hook}
70+
## Egy egyedi Horog kivonása {#extracting-a-custom-hook}
7171

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!
7373

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

7676
```js{3}
7777
import { useState, useEffect } from 'react';
@@ -94,11 +94,11 @@ function useFriendStatus(friendID) {
9494
}
9595
```
9696

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

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).
100100

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

103103
```js
104104
function useFriendStatus(friendID) {
@@ -110,20 +110,21 @@ function useFriendStatus(friendID) {
110110
}
111111
```
112112

113-
Now let's see how we can use our custom Hook.
113+
Most lássuk, hogy hogyan tudjuk használni az egyedi Horgunkat.
114114

115-
## Using a Custom Hook {#using-a-custom-hook}
115+
## Egyedi Horog használata {#using-a-custom-hook}
116116

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

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*:
120121

121122
```js{2}
122123
function FriendStatus(props) {
123124
const isOnline = useFriendStatus(props.friend.id);
124125
125126
if (isOnline === null) {
126-
return 'Loading...';
127+
return 'Betöltés...';
127128
}
128129
return isOnline ? 'Online' : 'Offline';
129130
}
@@ -141,19 +142,19 @@ function FriendListItem(props) {
141142
}
142143
```
143144

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.**
145146

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

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

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

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}
153154

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

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

158159
```js{8-9,13}
159160
const friendList = [
@@ -184,24 +185,24 @@ function ChatRecipientPicker() {
184185
}
185186
```
186187

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

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

191192
```js
192193
const [recipientID, setRecipientID] = useState(1);
193194
const isRecipientOnline = useFriendStatus(recipientID);
194195
```
195196

196-
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.
197198

198-
## `useYourImagination()` {#useyourimagination}
199+
## `useYourImagination()` (Használd a képzeleted) {#useyourimagination}
199200

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

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

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

206207
```js
207208
function todosReducer(state, action) {
@@ -211,16 +212,16 @@ function todosReducer(state, action) {
211212
text: action.text,
212213
completed: false
213214
}];
214-
// ... other actions ...
215+
// ... más akciók ...
215216
default:
216217
return state;
217218
}
218219
}
219220
```
220221

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

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

225226
```js
226227
function useReducer(reducer, initialState) {
@@ -235,7 +236,7 @@ function useReducer(reducer, initialState) {
235236
}
236237
```
237238

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

240241
```js{2}
241242
function Todos() {
@@ -249,4 +250,4 @@ function Todos() {
249250
}
250251
```
251252

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.

content/docs/nav.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
- id: hooks-rules
121121
title: Horgok szabályai
122122
- id: hooks-custom
123-
title: Készítsd el a saját horgod
123+
title: Saját Horgok készítése
124124
- id: hooks-reference
125125
title: Horog API referencia
126126
- id: hooks-faq

0 commit comments

Comments
 (0)