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: src/content/learn/responding-to-events.md
+45-45Lines changed: 45 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,56 +1,56 @@
1
1
---
2
-
title: Responding to Events
2
+
title: Réagir aux événements
3
3
---
4
4
5
5
<Intro>
6
6
7
-
React lets you add *event handlers* to your JSX. Event handlers are your own functions that will be triggered in response to interactions like clicking, hovering, focusing form inputs, and so on.
7
+
React vous permet d’ajouter des *gestionnaires d’événements* à votre JSX. Les gestionnaires d’événements sont vos propres fonctions qui seront déclenchées en réponse aux interactions de l’utilisateur telles que des clics, survols, activations de champs de saisie de formulaires, etc.
8
8
9
9
</Intro>
10
10
11
11
<YouWillLearn>
12
12
13
-
*Different ways to write an event handler
14
-
*How to pass event handling logic from a parent component
15
-
*How events propagate and how to stop them
13
+
*Différentes façons d’écrire un gestionnaire d’événements
14
+
*Comment passer la logique de gestion d’événements depuis un composant parent
15
+
*Comment les événements se propagent et comment les arrêter
## Ajouter des gestionnaires d’événements {/*adding-event-handlers*/}
20
20
21
-
To add an event handler, you will first define a function and then [pass it as a prop](/learn/passing-props-to-a-component)to the appropriate JSX tag. For example, here is a button that doesn't do anything yet:
21
+
Pour ajouter un gestionnaire d’événements, vous devrez d’abord définir une fonction et [la passer en tant que prop](/learn/passing-props-to-a-component)à la balise JSX appropriée. Par exemple, voici un bouton qui ne fait rien pour le moment :
22
22
23
23
<Sandpack>
24
24
25
25
```js
26
26
exportdefaultfunctionButton() {
27
27
return (
28
28
<button>
29
-
I don't do anything
29
+
Je ne fais rien
30
30
</button>
31
31
);
32
32
}
33
33
```
34
34
35
35
</Sandpack>
36
36
37
-
You can make it show a message when a user clicks by following these three steps:
37
+
Vous pouvez faire afficher un message après un clic sur le bouton en suivant ces trois étapes :
38
38
39
-
1. Declare a function called `handleClick` *inside* your `Button` component.
40
-
2. Implement the logic inside that function (use `alert` to show the message).
41
-
3. Add `onClick={handleClick}` to the `<button>` JSX.
39
+
1.Déclarez une fonction appelée`handleClick`*dans* votre composant `Button`.
40
+
2.Implémentez la logique de cette fonction (utilisez`alert`pour afficher le message).
41
+
3.Ajoutez`onClick={handleClick}`au `<button>` en JSX.
You defined the `handleClick` function and then [passed it as a prop](/learn/passing-props-to-a-component) to `<button>`. `handleClick` is an **event handler.** Event handler functions:
65
+
Vous avez défini la fonction `handleClick`puis l'avez [passée en tant que prop](/learn/passing-props-to-a-component)à`<button>`. `handleClick`est un**gestionnaire d'événements**. Les gestionnaires d'événements sont des fonctions qui :
66
66
67
-
* Are usually defined *inside* your components.
68
-
* Have names that start with `handle`, followed by the name of the event.
67
+
*Sont généralement définies à l'intérieur de vos composants.
68
+
*Ont des noms qui commencent par `handle`, suivi du nom de l'événement.
69
69
70
-
By convention, it is common to name event handlers as `handle` followed by the event name. You'll often see`onClick={handleClick}`, `onMouseEnter={handleMouseEnter}`, and so on.
70
+
Par convention, il est courant de nommer les gestionnaires d'événements en utilisant `handle`suivi du nom de l'événement. Vous verrez souvent`onClick={handleClick}`, `onMouseEnter={handleMouseEnter}`, et ainsi de suite.
71
71
72
-
Alternatively, you can define an event handler inline in the JSX:
72
+
Vous pouvez aussi définir un gestionnaire d'événements en ligne dans le JSX:
73
73
74
74
```jsx
75
75
<button onClick={functionhandleClick() {
76
-
alert('You clicked me!');
76
+
alert('Vous m’avez cliqué !');
77
77
}}>
78
78
```
79
79
80
-
Or, more concisely, using an arrow function:
80
+
Ou vous pouvez être plus concis en utilisant une fonction fléchée :
81
81
82
82
```jsx
83
83
<button onClick={() => {
84
-
alert('You clicked me!');
84
+
alert('Vous m’avez cliqué !');
85
85
}}>
86
86
```
87
87
88
-
All of these styles are equivalent. Inlineevent handlers are convenient for short functions.
88
+
Tous ces styles sont équivalents. Les gestionnaires d'événements en ligne sont pratiques pour les fonctions courtes.
89
89
90
90
<Pitfall>
91
91
92
-
Functions passed to event handlers must be passed, not called. For example:
92
+
Vous devez passer une fonction comme gestionnaire d'événements plutôt que de l'appeler. Par exemple :
93
93
94
-
|passing a function (correct) | calling a function (incorrect) |
94
+
|Passer une fonction (correct) |Appeler une fonction (incorrect) |
The difference is subtle. In the first example, the `handleClick`function is passed as an `onClick` event handler. This tells React to remember it and only call your function when the user clicks the button.
98
+
La différence est subtile. Dans le premier exemple, on passe la fonction `handleClick`comme gestionnaire d'événements à `onClick`. Ça indique à React de la mémoriser et d'appeler votre fonction uniquement lorsque l'utilisateur clique sur le bouton.
99
99
100
-
In the second example, the `()`at the end of `handleClick()`fires the function *immediately* during [rendering](/learn/render-and-commit), without any clicks. This is because JavaScript inside the [JSX`{`and`}`](/learn/javascript-in-jsx-with-curly-braces) executes right away.
100
+
Dans le deuxième exemple, on appelle la fonction `handleClick()`avec `()`à la fin, ce qui déclenche la fonction immédiatement pendant [le rendu](/learn/render-and-commit), sans aucun clic. C'est dû au fait que le code JavaScript à l'intérieur des [balises`{`et`}` du JSX](/learn/javascript-in-jsx-with-curly-braces)s'exécute immédiatement.
101
101
102
-
When you write code inline, the same pitfall presents itself in a different way:
102
+
Quand vous écrivez du code en ligne, le même écueil se présente de manière différente :
103
103
104
-
| passing a function (correct) | calling a function (incorrect) |
104
+
|Passer une fonction (correct) |Appeler une fonction (incorrect) |
Rather than executing the code inside with every render, this creates a function to be called later.
122
+
Au lieu d'exécuter le code à chaque rendu, ça crée une fonction à appeler ultérieurement.
123
123
124
-
In both cases, what you want to pass is a function:
124
+
Dans les deux cas, ce que vous souhaitez passer est une fonction :
125
125
126
-
* `<button onClick={handleClick}>` passes the `handleClick` function.
127
-
* `<button onClick={() =>alert('...')}>` passes the `() =>alert('...')` function.
126
+
*La fonction `handleClick` est passée à `<button onClick={handleClick}>`.
127
+
*La fonction `() => alert('...')` est passée à `<button onClick={() => alert('...')}>`.
128
128
129
-
[Read more about arrow functions.](https://javascript.info/arrow-functions-basics)
129
+
[Lisez pour en apprendre davantage sur les fonctions fléchées.](https://javascript.info/arrow-functions-basics)
130
130
131
131
</Pitfall>
132
132
133
-
### Reading props in event handlers {/*reading-props-in-event-handlers*/}
133
+
### Lire les props dans les gestionnaires d'événements {/*reading-props-in-event-handlers*/}
134
134
135
-
Because event handlers are declared inside of a component, they have access to the component's props. Here is a button that, when clicked, shows an alert with its `message`prop:
135
+
Puisque les gestionnaires d'événements sont déclarés à l'intérieur d'un composant, ils ont accès aux props du composant. Voici un bouton qui, en cliquant dessus, affiche une alerte avec sa prop `message` :
136
136
137
137
<Sandpack>
138
138
@@ -148,11 +148,11 @@ function AlertButton({ message, children }) {
0 commit comments