From 5ba9618239faf0838fac1b49e3cb0f13486f71c5 Mon Sep 17 00:00:00 2001 From: Alessio Minichiello Date: Wed, 20 Feb 2019 12:32:44 +0000 Subject: [PATCH 1/4] Translation for the page 'Handling Events' --- GLOSSARY.md | 1 + content/docs/handling-events.md | 80 ++++++++++++++++----------------- content/docs/nav.yml | 2 +- 3 files changed, 42 insertions(+), 41 deletions(-) diff --git a/GLOSSARY.md b/GLOSSARY.md index 7e4d67e7a..ca3904903 100644 --- a/GLOSSARY.md +++ b/GLOSSARY.md @@ -17,6 +17,7 @@ Glossary of the translations of technical and React-specific terms. - false - tag - lifecycle +- callback # Common Translations diff --git a/content/docs/handling-events.md b/content/docs/handling-events.md index a8d3a1f51..6da0c911c 100644 --- a/content/docs/handling-events.md +++ b/content/docs/handling-events.md @@ -1,6 +1,6 @@ --- id: handling-events -title: Handling Events +title: Gestione degli Eventi 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: +La gestione degli eventi negli elementi React è molto simile alla gestione degli eventi negli elementi DOM. Vi sono alcune differenze sintattiche: -* React events are named using camelCase, rather than lowercase. -* With JSX you pass a function as the event handler, rather than a string. +* Gli eventi React vengono dichiarati utilizzando camelCase, anziché in minuscolo. +* In JSX, il gestore di eventi (_event handler_) viene passato come funzione, piuttosto che stringa. -For example, the HTML: +Per esempio, l'HTML: ```html - ``` -is slightly different in React: +è leggermente diverso in React: ```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: +Un'altra differenza è che, in React, non è possibile ritornare `false` per impedire il comportamento predefinito. Devi chiamare `preventDefault` esplicitamente. Ad esempio, in un semplice codice HTML per impedire il comportamento predefinito del link di aprire una nuova pagina, potresti scrivere: ```html - - Click me + + Clicca qui ``` -In React, this could instead be: +In React, invece sarebbe: ```js{2-5,8} function ActionLink() { function handleClick(e) { e.preventDefault(); - console.log('The link was clicked.'); + console.log('Hai cliccato sul link.'); } return ( - Click me + Clicca qui ); } ``` -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. +In questo esempio, il parametro `e` è un evento sintetico (_synthetic event_). React definisce questi eventi sintetici in base alle [specifiche W3C](https://www.w3.org/TR/DOM-Level-3-Events/), quindi non hai bisogno di preoccuparti della compatibilità tra browser. Consulta la guida di riferimento [`SyntheticEvent`](/docs/events.html) per saperne di più. -When using React you should generally not 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. +Usando React, in generale, non dovresti aver bisogno di chiamare `addEventListener` per aggiungere listeners ad un elemento DOM dopo la sua creazione. Invece, basta fornire un listener quando l'elemento è inizialmente renderizzato. -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: +Quando definisci un componente usando una [classe ES6](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Classes), un pattern comune è usare un metodo della classe come gestore di eventi. Ad esempio, questo componente `Toggle` renderizza un pulsante che consente all'utente di alternare gli stati "ON" e "OFF": ```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 + // Necessario per accedere al corretto valore di `this` all'interno del callback this.handleClick = this.handleClick.bind(this); } @@ -91,64 +91,64 @@ ReactDOM.render( ); ``` -[**Try it on CodePen**](http://codepen.io/gaearon/pen/xEmzGg?editors=0010) +[**Provalo su CodePen**](http://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. +Fai attenzione al valore di `this` nei callback JSX. In JavaScript, i metodi delle classi non sono [associati](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_objects/Function/bind) (_bound_) di default. Se dimentichi di applicare `bind` a `this.handleClick` e di passarlo a `onClick`, `this` sarà `undefined` quando la funzione verrà effettivamente chiamata. -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. +Questo non è un comportamento specifico in React: è parte di [come funzionano le funzioni in JavaScript](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/). In generale, se ti riferisci ad un metodo senza `()` dopo di esso, per esempio `onClick = {this.handleClick}`, potresti aver bisogno di applicare `bind` a quel metodo. -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: +Se usare la chiamata al metodo `bind` ti sembra troppo, ci sono due alternative a disposizione. Puoi usare la sintassi sperimentale [proprietà pubbliche delle classi](https://babeljs.io/docs/plugins/transform-class-properties/), utilizzando le proprietà delle classi per associare correttamente i callback: ```js{2-6} class LoggingButton extends React.Component { - // This syntax ensures `this` is bound within handleClick. - // Warning: this is *experimental* syntax. + // Garantisce che `this` si riferisca all'oggetto originale all'interno di handleClick. +  // Attenzione: questa è sintassi *sperimentale*. handleClick = () => { - console.log('this is:', this); + console.log('Il valore di `this` è: ', this); } render() { return ( ); } } ``` -This syntax is enabled by default in [Create React App](https://github.com/facebookincubator/create-react-app). +Questa sintassi è abilitata nelle impostazioni predefinite di [Create React App](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: +Se non stai usando la sintassi delle proprietà delle classi, è possibile utilizzare una [funzione a freccia](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Functions/Arrow_functions) all'interno del callback: ```js{7-9} class LoggingButton extends React.Component { handleClick() { - console.log('this is:', this); + console.log('Il valore di `this` è: ', this); } render() { - // This syntax ensures `this` is bound within handleClick + // Questa sintassi garantisce che `this` sia associato correttamente all'interno di handleClick 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. +Il problema con questa sintassi è che viene creato un callback diverso ogni volta che `LoggingButton` viene renderizzato. Nella maggior parte dei casi, non vi sono problemi. Tuttavia, se questo callback viene passato come prop a componenti inferiori, tali componenti potrebbero eseguire un ulteriore re-renderizzamento. In generale, vi consigliamo di utilizzare `bind` nel costruttore o la sintassi delle proprietà pubbliche nelle classi, per evitare questo tipo di problema di prestazioni. -## Passing Arguments to Event Handlers {#passing-arguments-to-event-handlers} +## Passare Argomenti ai Gestori di Eventi {#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: +All'interno di un ciclo, è comune avere l'esigenza di passare un parametro aggiuntivo a un gestore di eventi. Ad esempio, avendo `id` come l'identificativo della riga, le seguenti dichiarazioni sarebbero entrambe valide: ```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. +Le due linee di codice precedenti sono equivalenti e utilizzano le [funzioni a freccia](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Functions_and_function_scope/Arrow_functions) e [`Function.prototype.bind`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind) rispettivamente. -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. +In entrambi i casi, l'argomento `e`, che rappresenta l'evento React, verrà passato come secondo argomento dopo l'ID. Con la funzione a freccia, devi passarlo esplicitamente, mentre con `bind` qualsiasi altro argomento viene passato automaticamente. diff --git a/content/docs/nav.yml b/content/docs/nav.yml index 326435cf5..e10441d06 100644 --- a/content/docs/nav.yml +++ b/content/docs/nav.yml @@ -22,7 +22,7 @@ - id: state-and-lifecycle title: State and Lifecycle - id: handling-events - title: Handling Events + title: Gestione degli Eventi - id: conditional-rendering title: Conditional Rendering - id: lists-and-keys From cc5cc3589a7da1a58bc6bed16ed537b03eaf674a Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Wed, 20 Feb 2019 17:07:53 +0000 Subject: [PATCH 2/4] Commit suggestion on 'callback' Co-Authored-By: lssmn --- content/docs/handling-events.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/handling-events.md b/content/docs/handling-events.md index 6da0c911c..f2457a8e9 100644 --- a/content/docs/handling-events.md +++ b/content/docs/handling-events.md @@ -138,7 +138,7 @@ class LoggingButton extends React.Component { } ``` -Il problema con questa sintassi è che viene creato un callback diverso ogni volta che `LoggingButton` viene renderizzato. Nella maggior parte dei casi, non vi sono problemi. Tuttavia, se questo callback viene passato come prop a componenti inferiori, tali componenti potrebbero eseguire un ulteriore re-renderizzamento. In generale, vi consigliamo di utilizzare `bind` nel costruttore o la sintassi delle proprietà pubbliche nelle classi, per evitare questo tipo di problema di prestazioni. +Il problema con questa sintassi è che viene creata una callback diversa ogni volta che `LoggingButton` viene renderizzato. Nella maggior parte dei casi, non vi sono problemi. Tuttavia, se questa callback viene passata come prop a componenti inferiori, tali componenti potrebbero eseguire un ulteriore re-renderizzamento. In generale, vi consigliamo di utilizzare `bind` nel costruttore o la sintassi delle proprietà pubbliche nelle classi, per evitare questo tipo di problema di prestazioni. ## Passare Argomenti ai Gestori di Eventi {#passing-arguments-to-event-handlers} From 480c87e12779ac486abdc69dfc0ad0e12051e5e8 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Wed, 20 Feb 2019 17:08:06 +0000 Subject: [PATCH 3/4] Commit suggestion Co-Authored-By: lssmn --- content/docs/handling-events.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/handling-events.md b/content/docs/handling-events.md index f2457a8e9..d8a67e9a7 100644 --- a/content/docs/handling-events.md +++ b/content/docs/handling-events.md @@ -142,7 +142,7 @@ Il problema con questa sintassi è che viene creata una callback diversa ogni vo ## Passare Argomenti ai Gestori di Eventi {#passing-arguments-to-event-handlers} -All'interno di un ciclo, è comune avere l'esigenza di passare un parametro aggiuntivo a un gestore di eventi. Ad esempio, avendo `id` come l'identificativo della riga, le seguenti dichiarazioni sarebbero entrambe valide: +All'interno di un ciclo, è comune avere l'esigenza di passare un parametro aggiuntivo ad un gestore di eventi. Ad esempio, avendo `id` come l'identificativo della riga, le seguenti dichiarazioni sarebbero entrambe valide: ```js From 8e2c4ea6ac32581ea11f0a03d1ee454123993a6f Mon Sep 17 00:00:00 2001 From: Alessio Minichiello Date: Wed, 20 Feb 2019 17:11:55 +0000 Subject: [PATCH 4/4] Fixed all 'callback' articles --- content/docs/handling-events.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/content/docs/handling-events.md b/content/docs/handling-events.md index d8a67e9a7..f038a71b1 100644 --- a/content/docs/handling-events.md +++ b/content/docs/handling-events.md @@ -66,7 +66,7 @@ class Toggle extends React.Component { super(props); this.state = {isToggleOn: true}; - // Necessario per accedere al corretto valore di `this` all'interno del callback + // Necessario per accedere al corretto valore di `this` all'interno della callback this.handleClick = this.handleClick.bind(this); } @@ -93,11 +93,11 @@ ReactDOM.render( [**Provalo su CodePen**](http://codepen.io/gaearon/pen/xEmzGg?editors=0010) -Fai attenzione al valore di `this` nei callback JSX. In JavaScript, i metodi delle classi non sono [associati](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_objects/Function/bind) (_bound_) di default. Se dimentichi di applicare `bind` a `this.handleClick` e di passarlo a `onClick`, `this` sarà `undefined` quando la funzione verrà effettivamente chiamata. +Fai attenzione al valore di `this` nelle callback JSX. In JavaScript, i metodi delle classi non sono [associati](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_objects/Function/bind) (_bound_) di default. Se dimentichi di applicare `bind` a `this.handleClick` e di passarlo a `onClick`, `this` sarà `undefined` quando la funzione verrà effettivamente chiamata. Questo non è un comportamento specifico in React: è parte di [come funzionano le funzioni in JavaScript](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/). In generale, se ti riferisci ad un metodo senza `()` dopo di esso, per esempio `onClick = {this.handleClick}`, potresti aver bisogno di applicare `bind` a quel metodo. -Se usare la chiamata al metodo `bind` ti sembra troppo, ci sono due alternative a disposizione. Puoi usare la sintassi sperimentale [proprietà pubbliche delle classi](https://babeljs.io/docs/plugins/transform-class-properties/), utilizzando le proprietà delle classi per associare correttamente i callback: +Se usare la chiamata al metodo `bind` ti sembra troppo, ci sono due alternative a disposizione. Puoi usare la sintassi sperimentale [proprietà pubbliche delle classi](https://babeljs.io/docs/plugins/transform-class-properties/), utilizzando le proprietà delle classi per associare correttamente le callback: ```js{2-6} class LoggingButton extends React.Component { @@ -119,7 +119,7 @@ class LoggingButton extends React.Component { Questa sintassi è abilitata nelle impostazioni predefinite di [Create React App](https://github.com/facebookincubator/create-react-app). -Se non stai usando la sintassi delle proprietà delle classi, è possibile utilizzare una [funzione a freccia](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Functions/Arrow_functions) all'interno del callback: +Se non stai usando la sintassi delle proprietà delle classi, è possibile utilizzare una [funzione a freccia](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Functions/Arrow_functions) all'interno della callback: ```js{7-9} class LoggingButton extends React.Component {