liveBook · Manning

onClick

As explained in React Quickly, Second Edition

onClick is a React event-handler property that specifies a callback for click events on an element or component.

onClick is the React event-handler property for responding to click events. Assign a callback expression to a JSX element or component, such as <button onClick={onClick}>Click me</button>. section 8.9 section 8.11

Use onClick to:

  • Run a callback when a user clicks an element. section 8.11
  • Receive an event object when the callback needs event details or must call methods such as preventDefault() or stopPropagation(). section 8.11
  • Open interactive UI, such as setting menu state to expanded. section 8.8.3

Opening a menu

function Menu() { const [isExpanded, setExpanded] = useState(false); return ( <button onClick={() => setExpanded(true)}> Show menu </button> ); } The callback updates state when the button is clicked. A complete expandable menu can combine this React handler with a window pointer-down listener, then prevent clicks inside the menu from closing it.

React event handlers are assigned through JSX properties rather than addEventListener on the JSX element. Events can propagate through the element tree, and regular JavaScript listeners remain available for HTML nodes or event types that React does not expose. section 8.10 section 8.11

How React Quickly, Second Edition covers it

How deep it goes
Defines JSX assignment, event objects, propagation, and default-action control, while deferring most implementation details.
Examples
Uses a button that opens an expandable menu and contrasts React handlers with window and menu pointer-down listeners.
What it assumes
Assumes JSX and callback syntax while teaching React event-handler properties and their distinction from manual DOM listeners.
What you can do after
Apply it via React to attach click callbacks, update component state, and control event behavior with the event object.
How it connects
Connects click handling with JSX, callback functions, event objects, state, event bubbling, preventDefault, stopPropagation, and DOM event listeners.

Also explained in

Compare how 2 books explain onClick →

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.