Event Handlers
Event handlers are functions that React runs when something happens in the interface, such as a click, a key press, or a change in an input. You attach them to JSX elements through props named in camelCase, like onClick and onChange, passing a function rather than a string. Combined with state, they are what make a React component interactive.
Attaching a handler
Define the function inside the component, usually named with a handle prefix, and pass it by reference to the event prop. React calls it for you when the event occurs, giving it an event object as the first argument.
import { useState } from "react";
function LikeButton() {
const [likes, setLikes] = useState(0);
function handleClick() {
setLikes((prev) => prev + 1);
}
function handleKeyDown(event) {
if (event.key === "Escape") setLikes(0);
}
return (
<div onKeyDown={handleKeyDown}>
<button type="button" onClick={handleClick}>
Like ({likes})
</button>
</div>
);
}
function ColourPicker({ colours, onPick }) {
return colours.map((colour) => (
<button
key={colour}
type="button"
onClick={() => onPick(colour)} // passing an argument
>
{colour}
</button>
));
}
Passing arguments to a handler
When a handler needs extra information, such as which colour was clicked, wrap the call in an arrow function: onClick={() => onPick(colour)}. The arrow function is what React stores and calls later; it then calls your function with the right argument. If you also need the event, accept it in the arrow function and forward it: onClick={(e) => handlePick(e, colour)}.
The synthetic event object
React passes a wrapper around the browser's native event, called a synthetic event. It has the familiar properties and methods: event.target for the element that triggered it, event.key for keyboard events, event.preventDefault() to stop default browser behaviour such as a form submit, and event.stopPropagation() to keep it from bubbling to parent handlers. It behaves consistently across browsers.
Calling instead of passing
The single most frequent bug is adding parentheses: onClick={handleClick()}. That calls the function immediately during rendering and passes its return value, usually undefined, as the handler. Clicking then does nothing. Worse, if the function updates state, as in onClick={setLikes(likes + 1)}, every render triggers another state update, and React stops with "Too many re-renders". Pass the function itself, or wrap it in an arrow function.
Another common slip is lowercase naming. onclick is the HTML attribute; in JSX it must be onClick. React warns about the unknown prop, but the button silently ignores clicks.
Handlers as props
Child components often cannot update the parent's state directly, so the parent passes a handler down as a prop. By convention, the prop is named onSomething (like onPick) and the parent's function handleSomething. The child calls the prop when its event happens, and the parent decides what to do. This keeps the child reusable: the same ColourPicker could change a theme in one place and a product colour in another.
Updating state from handlers
When the new state depends on the old one, use the updater form, as in setLikes((prev) => prev + 1). State updates are batched and applied on the next render, so reading likes right after calling the setter still gives the old value.
Takeaway: attach handlers with camelCase props, pass the function rather than calling it, use arrow functions to add arguments, read details from the synthetic event, and pass handlers down as onSomething props.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.