liveBook · Manning

Event target

As explained in React Quickly, Second Edition

An event target is the DOM node on which a browser event occurred or to which it was dispatched.

An event target is the DOM node where a browser event occurred or to which it was dispatched. In a React handler, event.target identifies that node. section 8.2.2 section 8.2.3

Use the target when one handler serves several interactive elements:

  • Compare event.target with a ref to determine which button caused the event. section 8.2.2
  • Read event.type when the same handler responds to different event kinds. section 8.2.2
  • Use common event methods such as preventDefault() and stopPropagation() when needed. section 8.2.3
Listing 8.4 Increment and decrement with a single event handler
import { useState, useRef } from "react";
function Counter() {
  const [counter, setCounter] = useState(0);
  const increment = useRef();                       #1
  const onClick = (evt) => {
    const delta =                                   #2
      evt.target === increment.current ? 1 : -1;    #2
    setCounter((value) => value + delta);           #3
  };
  return (
    <section>
      <h1>Value: {counter}</h1>
      <button ref={increment} onClick={onClick}>    #4
        Increment
      </button>
      <button onClick={onClick}>Decrement</button>  #4
    </section>
  );
}
function App() {
  return <Counter />;
}
export default App;

Inspect a counter that uses one onClick handler for both increment and decrement buttons, selecting the action through event.target. section 8.2.2

How React Quickly, Second Edition covers it

How deep it goes
Explains target comparisons with refs and distinguishes event types, but defers broader event propagation details.
Examples
Uses an increment-and-decrement counter whose shared handler compares event.target with an increment-button ref.
What it assumes
Assumes basic JavaScript event-handler knowledge while introducing React event objects and their common properties.
What you can do after
Apply it via React event handlers to select an action based on the control that triggered the event.
How it connects
Connects event targets with React event handlers, synthetic events, refs, and DOM nodes.

Also explained in

Compare how 3 books explain event target →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Quickly, Second Edition. The section links point to the parts of the book it draws on.