liveBook · Manning

onClick event

As explained in D3.js in Action, Third Edition

A React event handler property that responds to a user click on a component or DOM element.

An onClick event is a React event-handler property that responds when a user clicks a component or DOM element. It can pass the click result to a parent callback. section 8.7

Wire an onClick event through a component hierarchy:

  • Assign the parent callback to the child component’s onClick property. section 8.7
  • Pass the clicked button’s identifier to the parent handler. section 8.7
  • Update state only when the received identifier differs from the active filter. section 8.7

Filtering a rankings visualization

A filter button receives onClick={props.onFilterSelection}. The parent supplies filterSelectionHandler, which receives the clicked ID and calls setActiveFilter(id). Clicking a button then updates the rankings visualization. section 8.7

<Button
  key={filter.id}
  id={filter.id}
  label={filter.label}
  isActive={props.activeFilter === filter.id ? true : false}
  onClick={props.onFilterSelection}
/>

Inspect the complete Button usage, including the active state and callback passed through onClick. section 8.7

How D3.js in Action, Third Edition covers it

How deep it goes
Shows implementation across child props, parent callbacks, identifiers, and conditional state updates.
Examples
Demonstrates filter buttons changing a rankings visualization for an interactive dashboard.
What you can do after
Apply it via React to propagate clicks from reusable components into parent state changes.
How it connects
Connects React components, props, callbacks, state, and D3-managed visualization transitions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.