Handling events in React
How JSX wires DOM events to handler functions, what a synthetic event actually is, passing arguments to a handler, and when to call preventDefault.
Handling events in React
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Event handlers are passed to JSX attributes like `onClick` as a function reference, not a string, and never called directly in the attribute — `onClick={handleClick}`, not `onClick={handleClick()}`.
- React wraps native browser events in a SyntheticEvent, a cross-browser wrapper with the same interface everywhere, so you don't write separate code paths for different browsers.
- To pass extra information to a handler, wrap the call in an inline arrow function — `onClick={() => handleDelete(item.id)}` — rather than trying to invoke the handler with arguments directly in the attribute.
- Calling `event.preventDefault()` stops a browser's default reaction to an event, most commonly a form's full-page submit-and-reload, so your own JavaScript can handle it instead.
- An inline arrow function in JSX creates a brand-new function on every render, which is fine for most components but worth knowing once performance work starts later in this roadmap.
- Event names in JSX are camelCase (`onClick`, `onChange`, `onSubmit`), matching JavaScript convention rather than the lowercase HTML attribute names like `onclick`.
Wiring a function to an event
A React component looks like HTML with event attributes bolted on, but the wiring works differently from plain HTML. In plain HTML you’d write onclick="doThing()" as a string that gets evaluated. In JSX, event props take an actual function value:
function SaveButton() {
function handleClick() {
console.log("saved");
}
return <button onClick={handleClick}>Save</button>;
}
handleClick here is a reference, not an invocation. React stores that reference and calls it itself exactly once, when the click actually happens. The single most common beginner mistake is writing onClick={handleClick()} — the parentheses call the function immediately, during render, and then hand React whatever that call returned (usually undefined) as the handler. The button ends up with no real click behavior, and handleClick has already run once for no reason.
Event prop names follow camelCase — onClick, onChange, onSubmit, onMouseEnter — mirroring how you’d already name a JavaScript callback, rather than the lowercase onclick you’d see in an HTML attribute.
What you actually receive: the synthetic event
When a handler fires, React calls it with an event object as the argument:
function handleClick(event) {
console.log(event.target); // the DOM node that was clicked
}
That event is not the raw browser event — it’s a SyntheticEvent, a thin wrapper React creates around the native event so that event.target, event.preventDefault(), and the rest of the API behave identically across browsers. Historically this also let React pool and reuse event objects for performance; modern React (17+) no longer pools them, but the cross-browser normalization is still the reason the wrapper exists. You almost never need to reach for the underlying native event, but it’s available as event.nativeEvent on the rare occasion you do.
Passing arguments to a handler
A handler attached directly, onClick={handleDelete}, only ever receives the event object — there’s no way to also hand it an id or index. The fix is to wrap the call in an inline arrow function, which is itself a valid function reference for React to call:
function TodoItem({ id, text, onDelete }) {
return (
<li>
{text}
<button onClick={() => onDelete(id)}>Delete</button>
</li>
);
}
React still calls a function reference — the arrow function — on click. That arrow function then calls onDelete(id) with whatever id it closed over from the surrounding scope. This pattern, an inline arrow function closing over a loop variable or prop, is how almost every “click this specific row” interaction in React is built, and it comes up constantly once you’re rendering lists.
Stopping the browser’s own reaction
Some DOM elements have built-in behavior on certain events: a link navigates on click, a form does a full-page submit-and-reload on submit. event.preventDefault() cancels that default action without stopping your own handler code from running:
function LoginForm() {
function handleSubmit(event) {
event.preventDefault();
// read form values and do something with them in JS,
// instead of letting the browser reload the page
}
return (
<form onSubmit={handleSubmit}>
<button type="submit">Log in</button>
</form>
);
}
Without preventDefault(), that click would trigger the form’s native submit, the browser would navigate (usually to the same URL), and every bit of component state — including whatever the user just typed — would be wiped out by the reload. This comes up again, with the actual input values, in the forms lesson later in this group.
What the video covers
Codevolution’s episode walks through attaching handlers in JSX using a current React 19 example: a component that renders several buttons from a list and logs a different message depending on which one was clicked, which is the inline-arrow-function argument-passing pattern covered above in practice. It moves quickly and doesn’t stop to unpack what a SyntheticEvent actually is under the hood, or dwell on preventDefault specifically — this lesson’s middle two sections, and the deeper video from Sam Meech-Ward, fill that in.
Where this fits
Next, you’ll use these same handler-wiring skills to update data that actually changes what’s on screen, in useState and component state.
Check yourself
What is wrong with writing `<button onClick={handleClick()}>Save</button>`?
The parentheses call `handleClick` immediately during render instead of passing a reference to React, so the function runs once on every render rather than once when the button is clicked. The attribute needs a function reference, `onClick={handleClick}`, or an inline function that calls it, `onClick={() => handleClick()}`.
How do you pass an argument, like an item's id, to a click handler in JSX?
Wrap the call in an inline arrow function so React still receives a function reference: `onClick={() => handleDelete(item.id)}`. The arrow function is what gets called on click, and it closes over `item.id` to call your handler with it.
What is a SyntheticEvent, and why does React use one instead of handing you the raw browser event?
A SyntheticEvent is React's cross-browser wrapper around the native DOM event, exposing the same properties and methods (like `preventDefault` and `target`) regardless of which browser fired the underlying event. It lets you write one handler instead of branching on browser quirks.
Why would you call `event.preventDefault()` inside a form's submit handler?
Submitting a form normally makes the browser do a full page navigation and reload, discarding any component state. Calling `preventDefault()` on the submit event stops that default behavior so your handler can process the form data in JavaScript without the page reloading.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.