React Events
React Events
React Events
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
React Events
❮ Previous Next ❯
Just like HTML DOM events, React can perform actions based on user events.
React has the same events as HTML: click, change, mouseover etc.
Adding Events
React events are written in camelCase syntax:
React:
HTML:
https://www.w3schools.com/REACT/react_events.asp 1/8
4/2/25, 10:42 a.m. React Events
<buttonTutorials
onclick="shoot()">Take
Exercises the Shot!</button>
Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Example:
Put the shoot function inside the Football component:
function Football() {
const shoot = () => {
alert("Great Shot!");
}
return (
<button onClick={shoot}>Take the shot!</button>
);
}
Run Example »
Get Certified!
school
w3 s
5
CE
02
TI 2
Complete the React modules, do the exercises, take the exam and become
R
FI .
ED
w3schools certified!!
$95 ENROLL
Passing Arguments
https://www.w3schools.com/REACT/react_events.asp 2/8
4/2/25, 10:42 a.m. React Events
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Example:
Send "Goal!" as a parameter to the shoot function, using arrow function:
function Football() {
const shoot = (a) => {
alert(a);
}
return (
<button onClick={() => shoot("Goal!")}>Take the shot!</button>
);
}
Run Example »
Example:
Arrow Function: Sending the event object manually:
function Football() {
const shoot = (a, b) => {
alert(b.type);
/*
'b' represents the React event that triggered the function,
https://www.w3schools.com/REACT/react_events.asp 3/8
4/2/25, 10:42 a.m. React Events
Run Example »
?
Exercise
In React, which is a correct way of adding a mouseover event to trigger a
function called 'run_now'?
Submit Answer »
❮ Previous Next ❯
https://www.w3schools.com/REACT/react_events.asp 4/8
4/2/25, 10:42 a.m. React Events
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
COLOR PICKER
https://www.w3schools.com/REACT/react_events.asp 5/8
4/2/25, 10:42 a.m. React Events
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
https://www.w3schools.com/REACT/react_events.asp 6/8
4/2/25, 10:42 a.m. React Events
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
HTML
CSS JAVASCRIPT
SQL PYTHON JAVA PHP HOW TO W3.CSS C
https://www.w3schools.com/REACT/react_events.asp 8/8