Skip to content
ClapelloNotes by Barath
ReactCh 14 · 2/6
Core4 min readUpdated

React / 77

The React event object

TL;DR

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

fired on the element you clicked, e.g. a PointerEvent
caught by React's listener at the root

2 · React wraps it

The React event object (a “synthetic event”)

e.type"click"
e.targetwhere the click started
e.currentTargetelement whose handler is running
e.preventDefault()stop the browser's default action
e.stopPropagation()stop handlers further up
e.nativeEventthe original browser event
handed to each matching handler, innermost first

3 · your code

function handleClick(e) { … }
React listens once, at the root of your app. When a browser event arrives, React wraps it in a React event object and calls the handlers of the components involved.

The fields you'll actually use

FieldWhat it isTypical use
e.targetThe element the event started one.target.value, e.target.checked, e.target.name
e.currentTargetThe element whose handler is runningThe form in onSubmit, the button even if its icon was clicked
e.typeThe event name"click", "change", "keydown"
e.keyThe key pressed (keyboard events only)if (e.key === "Enter") …
e.preventDefault()Stops the browser's default actionForm submits, link clicks
e.stopPropagation()Stops handlers on parent elementsNext topic
e.nativeEventThe browser's original eventRare: 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.

Click the heart, then the word, then the button's edge. e.target follows your finger; e.currentTarget is always the button that owns onClick. Tab to it and press Enter: the target is the button itself.
LikeButton.jsx
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

EventPlain browserReact
onChange on text inputschange fires when you leave the fieldFires on every keystroke (like the input event)
onFocus / onBlurfocus and blur don't bubbleThey bubble up to parents
onScrollDoesn't bubbleDoesn't bubble either

The onChange difference is why controlled inputs work: React hears about every key.

In TypeScript

HandlerType of e
onClick on a buttonReact.MouseEvent<HTMLButtonElement>
onChange on an inputReact.ChangeEvent<HTMLInputElement>
onKeyDownReact.KeyboardEvent<HTMLInputElement>
onSubmit on a formReact.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.