React / 77
The React event object
Your handler gets a React event object, also called a synthetic event. It wraps the browser's event with the same standard fields, and the original is in e.nativeEvent.
What e is
Your handlers receive a React event object, often called a synthetic event. React builds it from the browser's own (native) event. It follows the same standard as DOM events, so the names you know from plain JavaScript work, but it also irons out differences between browsers.
1 · the browser
A native click event
PointerEvent2 · React wraps it
The React event object (a “synthetic event”)
e.type"click"e.targetwhere the click startede.currentTargetelement whose handler is runninge.preventDefault()stop the browser's default actione.stopPropagation()stop handlers further upe.nativeEventthe original browser event3 · your code
function handleClick(e) { … }The fields you'll actually use
| Field | What it is | Typical use |
|---|---|---|
e.target | The element the event started on | e.target.value, e.target.checked, e.target.name |
e.currentTarget | The element whose handler is running | The form in onSubmit, the button even if its icon was clicked |
e.type | The event name | "click", "change", "keydown" |
e.key | The key pressed (keyboard events only) | if (e.key === "Enter") … |
e.preventDefault() | Stops the browser's default action | Form submits, link clicks |
e.stopPropagation() | Stops handlers on parent elements | Next topic |
e.nativeEvent | The browser's original event | Rare: something React doesn't expose |
Mouse events add things like clientX, keyboard events add key and shiftKey. They're the standard DOM names.
target vs currentTarget
What's inside e?
Try it<button onClick={handleClick}> <span class="icon">♥</span> <span class="label">Like</span> </button>
Click somewhere on the button.
export default function LikeButton() {
function handleClick(e) {
console.log(e.target); // the <span> you hit, or the button
console.log(e.currentTarget); // always the <button>
}
return (
<button onClick={handleClick}>
<span className="icon">♥</span> Like
</button>
);
}When a handler sits on a parent with children inside it, use e.currentTarget to mean "the element I put this handler on".
Where React actually listens
React doesn't attach a listener to every button. It adds its listeners once, at the root of your app (the container you passed to createRoot). When a click reaches the root, React works out which components' handlers should run and calls them in order, innermost first. This trick is called event delegation.
React fixes up the fields so this is invisible: e.currentTarget is your element, not the root. Only e.nativeEvent.currentTarget gives the game away.
Where React differs from the browser
| Event | Plain browser | React |
|---|---|---|
onChange on text inputs | change fires when you leave the field | Fires on every keystroke (like the input event) |
onFocus / onBlur | focus and blur don't bubble | They bubble up to parents |
onScroll | Doesn't bubble | Doesn't bubble either |
The onChange difference is why controlled inputs work: React hears about every key.
In TypeScript
| Handler | Type of e |
|---|---|
onClick on a button | React.MouseEvent<HTMLButtonElement> |
onChange on an input | React.ChangeEvent<HTMLInputElement> |
onKeyDown | React.KeyboardEvent<HTMLInputElement> |
onSubmit on a form | React.SubmitEvent<HTMLFormElement> |
Recent @types/react versions mark React.FormEvent as deprecated. Use ChangeEvent for onChange and SubmitEvent for onSubmit instead. (On an older version that has no SubmitEvent yet, FormEvent is still fine.)
Gotchas
Recall
Can you answer these?
0/5
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
