In web development, events are actions that occur when the user interacts with your app. Common examples of events include clicking a button, hovering over an element, or submitting a form. React allows you to handle these events in a structured way using event handlers.
In this lesson, we will focus on handling button clicks. This is a simple and important concept for beginners because it helps make your app interactive.
Before we dive into examples, let’s cover a couple of important differences between how React handles events compared to regular HTML and JavaScript:
onClick
in React instead of onclick
in regular HTML.onclick="doSomething()"
, but in React, you pass the function directly without quotes.These differences make React event handling more powerful and flexible.
Let’s start with a very simple example where we respond to a button click. When the user clicks the button, we’ll display a message.
import React from 'react';
function App() {
// This function will run when the button is clicked
function handleClick() {
alert('Button was clicked!');
}
return (
<div>
<button onClick={handleClick}>Click Me</button>
</div>
);
}
export default App;
onClick
event directly to the button. Whenever the button is clicked, the event triggers the handleClick
function, which then shows the alert message.Notice that in React, the event name is onClick
(camelCase) instead of the lowercase onclick
that you might have seen in regular HTML.
Why are we passing the function directly? In React, instead of wrapping the function name in quotes like "handleClick()"
, we just pass the function name handleClick
without parentheses. This is because we want the function to run only when the button is clicked, not immediately when the page loads.
There may be situations where you want to handle different actions based on which button is clicked. For example, imagine you have multiple buttons on the page, and you want to know exactly which button the user clicked. To handle this, you can pass arguments (or values) to the event handler.
import React from 'react';
function App() {
// This function accepts a 'buttonName' and shows it in the alert
function handleClick(buttonName) {
alert(`${buttonName} button was clicked!`);
}
return (
<div>
<button onClick={() => handleClick('First')}>First Button</button>
<button onClick={() => handleClick('Second')}>Second Button</button>
</div>
);
}
export default App;
buttonName
. When the button is clicked, this function shows which button was clicked by displaying a message like "First button was clicked!" or "Second button was clicked!".handleClick
function, we use an arrow function inside the onClick
event: onClick={() => handleClick("First")}
. This allows us to pass the string 'First' to the function when the first button is clicked, and 'Second' when the second button is clicked.Why use an arrow function? If you tried to write onClick={handleClick("First")}
, the function would run immediately when the page loads, and we don’t want that. By using the arrow function () => handleClick('First')
, it only runs when the button is actually clicked.
React uses something called Synthetic Events to handle events across different browsers in a consistent way. This means you don’t have to worry about browser differences when handling events. While you don’t need to dive into the technical details of synthetic events as a beginner, it’s good to know that React handles some of the behind-the-scenes work for you, making things easier and more predictable.
While we’ve focused on the onClick
event here, React supports many other events that you might use depending on what you’re building. Here are a few common ones:
onMouseEnter
: Runs when the user hovers their mouse over an element.onMouseLeave
: Runs when the user stops hovering over an element.onKeyDown
: Runs when the user presses a key on their keyboard.onFocus
: Runs when an element (like an input field) gains focus.onBlur
: Runs when an element loses focus (e.g., when a user clicks outside an input field).In this lesson, we’ve covered the basics of handling events in React functional components. We learned how to:
onClick
to elements in React.This is an important part of building interactive web apps, and mastering event handling will give you more control over how your app behaves.
onClick
event to a button and run a function when the user clicks it.onClick
.onClick
, onMouseEnter
, onKeyDown
, and more, giving you control over different user interactions.By practicing handling events, you’ll be able to make your React apps interactive and dynamic!