Responding to events
onClick and friends, passing handlers as props, event objects, preventDefault and propagation.
Interactive apps respond to people: clicks, typing, hovering, submitting forms, pressing keys. In React you attach event handlers directly in JSX. This lesson covers how to write them, pass them around, read event details, and control default behaviour and propagation.
Adding an event handler#
- Define a function inside your component (by convention,
handleSomething). - Pass it to the element's event prop —
onClick,onChange,onSubmit…
Event props are camelCase and take a function, not a string.
Pass the function, don't call it
The second line runs handleClick immediately while rendering — and if the handler updates state, it causes an infinite render loop.
Inline handlers
Short handlers can be written inline:
Inline arrows are fine and idiomatic. Creating a new function per render is cheap.
Passing arguments#
Wrap the call in an arrow function:
The event object#
React passes an event object to your handler. It wraps the browser's native event with the same interface (target, preventDefault(), key, clientX…), and works consistently across browsers:
e.nativeEvent gives you the underlying browser event if you ever need it.
Common events
In React,
onChangeon an input fires on every keystroke (like the DOM'sinputevent), not only when the field loses focus.
Passing handlers as props#
Most handlers live in a parent and are passed down. Name the prop after what happened (onPlay, onDelete) rather than the DOM event, so the component describes its own interactions:
Button doesn't know what clicking means — that's decided by whoever uses it. This keeps components reusable.
Preventing default behaviour#
Some events have built-in browser behaviour. A <form> submission reloads the page; a link navigates. Call e.preventDefault() to cancel it:
Listen for onSubmit on the form — it also fires when the user presses Enter. (React 19 also lets you pass a function to a form's action prop; that's covered in the React 19 lesson.)
Propagation: events bubble up#
Events travel up from the element that was clicked through all its ancestors. Both handlers below run when you click the button:
e.stopPropagation()stops the event reaching parent handlers.e.preventDefault()cancels the browser's default action. They're unrelated — you might need either, both or neither.
To handle an event during the capture phase (before children), add Capture: onClickCapture.
Handlers can do anything — except mutate during render#
Event handlers are the right place for side effects: updating state, sending requests, navigating, logging. Rendering code (the body of your component) should stay pure. In the next lesson, handlers will update state, which makes React re-render with new values.
Accessibility: use the right element#
Use <button> for actions and <a href> (or a router Link) for navigation.
Common mistakes#
onClick={handleClick()}— calling instead of passing.onclick(lowercase) — React ignores it and warns.- Returning
falseto prevent default — calle.preventDefault(). - Using
stopPropagationwhen you meantpreventDefault, or vice versa. - Forgetting
type="button"on buttons inside forms: the default type issubmit.
What's next#
Handlers that only log aren't very exciting. Next, give components memory with state using useState.
Check your understanding
Quick quiz
1.What is wrong with
<button onClick={handleClick()}>?2.How do you stop a form's
onSubmitfrom reloading the page?3.A
<button>inside a clickable<li>triggers both click handlers. How do you stop the<li>handler from running?
Finished reading?
Mark this lesson complete to track your progress.