diff --git a/content/docs/handling-events.md b/content/docs/handling-events.md index abf61eab8..e5cf5f448 100644 --- a/content/docs/handling-events.md +++ b/content/docs/handling-events.md @@ -1,6 +1,6 @@ --- id: handling-events -title: Handling Events +title: Események kezelése permalink: docs/handling-events.html prev: state-and-lifecycle.html next: conditional-rendering.html @@ -8,57 +8,57 @@ redirect_from: - "docs/events-ko-KR.html" --- -Handling events with React elements is very similar to handling events on DOM elements. There are some syntactic differences: +A React elemek eseményeinek kezelése nagyon hasonló a DOM elemek eseménykezeléséhez. Viszont van néhány szintaxisbeli különbség: -* React events are named using camelCase, rather than lowercase. -* With JSX you pass a function as the event handler, rather than a string. +* A React események elnevezésére camelCase-t használunk kisbetűk helyett. +* A JSX-ben string helyett függvényt adunk át az eseménykezelőnek. -For example, the HTML: +Például ez a HTML: ```html ``` -is slightly different in React: +kissé máshogyan néz ki Reactben: ```js{1} ``` -Another difference is that you cannot return `false` to prevent default behavior in React. You must call `preventDefault` explicitly. For example, with plain HTML, to prevent the default link behavior of opening a new page, you can write: +Egy másik különbség, hogy Reactben `false` érték visszaadásával nem tudod megakadályozni az alapviselkedést. Határozottan meg kell hívni a `preventDefault`-ot. Egyszerű HTML-ben például egy link alapviselkedésének megváltoztatásához írhatjuk ezt: ```html - - Click me + + Kattints rám ``` -In React, this could instead be: - +Reactben ez így nézne ki: +**** ```js{2-5,8} function ActionLink() { function handleClick(e) { e.preventDefault(); - console.log('The link was clicked.'); + console.log('Rákattintottak a linkre.'); } return ( - Click me + Kattints rám ); } ``` -Here, `e` is a synthetic event. React defines these synthetic events according to the [W3C spec](https://www.w3.org/TR/DOM-Level-3-Events/), so you don't need to worry about cross-browser compatibility. See the [`SyntheticEvent`](/docs/events.html) reference guide to learn more. +Itt az `e` egy szintetikus esemény. A React ezeket a szintetikus eseményeket a [W3C specifikáció](https://www.w3.org/TR/DOM-Level-3-Events/) alapján definiálja, emiatt nem kell a böngészők közötti kompatibilitással törődnöd. Lásd a [`SyntheticEvent`](/docs/events.html) referenciát, ha többet szeretnél megtudni erről. -When using React, you generally don't need to call `addEventListener` to add listeners to a DOM element after it is created. Instead, just provide a listener when the element is initially rendered. +Reactben általában nem kell meghívnod az `addEventListener`-t hogy eseménykezelőket adj hozzá egy DOM elemhez miután az létrejött. Ehelyett szimplán akkor add hozzá, amikor az elem először renderelődik. -When you define a component using an [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes), a common pattern is for an event handler to be a method on the class. For example, this `Toggle` component renders a button that lets the user toggle between "ON" and "OFF" states: +Amikor egy komponenst [ES6 osztályként](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) definiálsz, az eseménykezelő általában egy függvény az adott osztályban. Például ez a `Toggle` komponens egy gombot renderel, ami az "ON" és "OFF" közötti kapcsolást teszi lehetővé a felhasználó számára: ```js{6,7,10-14,18} class Toggle extends React.Component { @@ -66,7 +66,7 @@ class Toggle extends React.Component { super(props); this.state = {isToggleOn: true}; - // This binding is necessary to make `this` work in the callback + // Ez a kötés(binding) szükséges a `this` használatához a callbackben this.handleClick = this.handleClick.bind(this); } @@ -91,18 +91,18 @@ ReactDOM.render( ); ``` -[**Try it on CodePen**](https://codepen.io/gaearon/pen/xEmzGg?editors=0010) +[**Próbáld ki CodePenen**](https://codepen.io/gaearon/pen/xEmzGg?editors=0010) -You have to be careful about the meaning of `this` in JSX callbacks. In JavaScript, class methods are not [bound](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind) by default. If you forget to bind `this.handleClick` and pass it to `onClick`, `this` will be `undefined` when the function is actually called. +Légy óvatos a `this` használatával a JSX callbackekben. A JavaScriptben az osztálymetódusok alapesetben nincsenek [hozzákötve](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind) a `this`-hez. Ha elfelejtenéd hozzákötni a `this.handleClick`-et a `this`-hez és így rendeled hozzá az `onClick`-hez, a `this` `undefined` értékű lesz amikor a metódus meghívódik. -This is not React-specific behavior; it is a part of [how functions work in JavaScript](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/). Generally, if you refer to a method without `()` after it, such as `onClick={this.handleClick}`, you should bind that method. +Ez nem React-specifikus viselkedés, hanem alapvetően [így működnek a függvények JavaScriptben](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/). Általában, ha egy metódusra a `()` nélkül hivatkozol, például `onClick={this.handleClick}`, hozzá kell kötnöd ezt a metódust a `this`-hez. -If calling `bind` annoys you, there are two ways you can get around this. If you are using the experimental [public class fields syntax](https://babeljs.io/docs/plugins/transform-class-properties/), you can use class fields to correctly bind callbacks: +Ha a `bind` hívás zavar téged, kétféle módon is kikerülheted. Ha a kísérleti [nyilvános osztálymező szintaxist](https://babeljs.io/docs/plugins/transform-class-properties/) használod, a kötéshez osztálymezőket is használhatsz: ```js{2-6} class LoggingButton extends React.Component { - // This syntax ensures `this` is bound within handleClick. - // Warning: this is *experimental* syntax. + // Ez a szintaxis biztosítja a `this` handleClickhez kötését. + // Vigyázat: ez *kísérleti* szintaxis. handleClick = () => { console.log('this is:', this); } @@ -110,16 +110,16 @@ class LoggingButton extends React.Component { render() { return ( ); } } ``` -This syntax is enabled by default in [Create React App](https://github.com/facebookincubator/create-react-app). +Ez a szintaxis alapból be van kapcsolva [Create React App-ban](https://github.com/facebookincubator/create-react-app). -If you aren't using class fields syntax, you can use an [arrow function](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions) in the callback: +Az osztálymező szintaxis helyett [nyilas metódusokat](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions) is használhatsz a callbackben: ```js{7-9} class LoggingButton extends React.Component { @@ -128,27 +128,27 @@ class LoggingButton extends React.Component { } render() { - // This syntax ensures `this` is bound within handleClick + // Ez a szintaxis biztosítja a `this` handleClickhez való kötését return ( ); } } ``` -The problem with this syntax is that a different callback is created each time the `LoggingButton` renders. In most cases, this is fine. However, if this callback is passed as a prop to lower components, those components might do an extra re-rendering. We generally recommend binding in the constructor or using the class fields syntax, to avoid this sort of performance problem. +A probléma ezzel a szintaxissal az, hogy a `LoggingButton` minden egyes renderelésekor egy új callbacket hozunk létre. A legtöbb esetben ez nem gond. Viszont ha ezt a callbacket gyermek komponenseknek adjuk tovább, azok feleslegesen renderelődhetnek újra. Az efféle teljesítményproblémák elkerülése érdekében általában a konstruktoron belüli kötést vagy az osztálymező szintaxis használatát ajánljuk. -## Passing Arguments to Event Handlers {#passing-arguments-to-event-handlers} +## Argumentumok átadása az eseménykezelőknek {#passing-arguments-to-event-handlers} -Inside a loop, it is common to want to pass an extra parameter to an event handler. For example, if `id` is the row ID, either of the following would work: +Egy cikluson belül gyakori, hogy egy extra paramétert is szeretnénk átadni egy eseménykezelőnek. Például ha `id` a sor azonosítója, a következők bármelyike működhet: ```js - - + + ``` -The above two lines are equivalent, and use [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) and [`Function.prototype.bind`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind) respectively. +A fenti két sor ekvivalens, és sorrendben [nyilas függvényeket](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) és [`Function.prototype.bind`-ot](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind) használnak. -In both cases, the `e` argument representing the React event will be passed as a second argument after the ID. With an arrow function, we have to pass it explicitly, but with `bind` any further arguments are automatically forwarded. +Mindkét esetben az `e` argumentum, ami a React eseményt reprezentálja, az ID után egy második paraméterként lesz átadva. A nyilas függvénnyel explicit módon adjuk át, viszont a `bind` használatával minden további argumentum automatikusan átadódik.