Events and Interaction

Event handling is application behavior design. Explicit, accessible interaction paths keep UI logic predictable and testable.

Event binding basics

React attaches event handlers using camelCase props such as onClick, onChange, and onSubmit. Pass a function reference rather than calling the function during render—writing onClick={handleSave()} executes immediately on every render instead of waiting for a click. React wraps native browser events in a SyntheticEvent object so properties like event.preventDefault() and event.stopPropagation() behave consistently across browsers:

function SaveButton({ onSave, isSaving }) {
  // Pass the handler reference; React invokes it only when the user clicks
  return (
    <button type="button" onClick={onSave} disabled={isSaving}>
      {isSaving ? "Saving..." : "Save changes"}
    </button>
  );
}

Passing event and arguments

When a handler needs item-specific context—such as deleting a specific row in a list—wrap the call in an inline arrow function so the argument is captured without invoking the handler at render time. Keep both the event object and domain identifier explicit when you need to stop propagation or inspect modifier keys:

function TaskRow({ task, onSelect, onDelete }) {
  function handleDeleteClick(event) {
    // Prevent row selection when the user clicks the nested delete button
    event.stopPropagation();
    onDelete(task.id);
  }

  return (
    <li onClick={() => onSelect(task.id)}>
      <span>{task.title}</span>
      <button type="button" onClick={handleDeleteClick} aria-label={`Delete ${task.title}`}>
        Delete
      </button>
    </li>
  );
}

Event delegation strategy

Under the hood, React already delegates events to the root container rather than attaching individual DOM listeners to every element. Still, in large repeated tables or grids, centralizing callbacks at the container level—passing a single onAction(actionType, rowId) prop or reading data-* attributes—reduces prop plumbing and keeps child row components focused on rendering.

Interaction architecture

Separate intent from presentation: a button or menu item should emit a domain-meaningful callback like onArchiveOrder(orderId) rather than embedding API calls or toast notifications inside presentational markup. Use native interactive elements (<button>, <a>, <input>) instead of clickable <div> tags so keyboard navigation (Enter, Space, Tab) and screen-reader semantics work out of the box.

Professional tips

Keep interaction flows resilient under real user behavior:

  1. Keep handlers small and deterministic; delegate multi-step workflows to helper functions or hooks.
  2. Guard against double-clicks on async actions by disabling the trigger or tracking pending state.
  3. Never attach click handlers to non-interactive elements like <div> or <span> when a <button> fits.

Practice

Build an interactive task list where clicking a row opens details, clicking a nested delete button asks for confirmation without triggering row selection, and keyboard users can operate every action.