event handler
A function that runs when something happens to an element, such as a click (onClick) or a key press (onKeyDown).
In more detail
When a component merges its own handler with a consumer's, it should call both, usually the consumer's first, and skip its own if the consumer called event.preventDefault(). Replacing one with the other silently breaks behaviour.
Example
<button onClick={() => setOpen(true)}>Open</button>Related terms
- callback function — A function you hand to other code so that it can call it later, for example when a timer fires or an event happens.
- event bubbling — The way an event on an element also triggers handlers on its parent elements, one level at a time up the tree.
- preventDefault — A method on an event that cancels the browser's built-in action, such as submitting a form or following a link.
- async void — An async method that returns void instead of Task, so its caller cannot await it or catch its exceptions.
- delegate — A type that holds a reference to a method, so a method can be stored in a variable and called later.
- Client Component — A component whose code is also sent to the browser so it can use state, effects and event handlers.
- hydration — The step where React in the browser attaches event handlers and state to HTML that was already rendered on the server.
- Server Functions — Async functions marked with "use server" that run on the server but can be called from client components, for example as a form action.
- SyntheticEvent — The event object React passes to your handlers, a wrapper around the browser native event that behaves the same in every browser.
- side effect — Anything a piece of code does besides returning a value, such as writing to a database, sending a message or changing a variable outside it.
- useEffect — The React hook for running side work, such as adding listeners or changing document.body, after a component renders.
Taught in
- Headless Architecture: State vs Presentation Separation — Design Systems & Component Engineering
- Polymorphism & Slotted Composition: The asChild Pattern — Design Systems & Component Engineering
- Focus Management: Trapping, Restoration & Focus Rings — Design Systems & Component Engineering
- Keyboard Navigation Protocols: Roving Tabindex & Virtual Focus — Design Systems & Component Engineering
- Async and Await: Managing Concurrency — Modern .NET Core, C# & Enterprise Microservices
- Server Components: Async Execution & Constraints — Next.js 15 & Modern App Router Architecture
- Client Components: use client & Hydration Models — Next.js 15 & Modern App Router Architecture
- Server Actions: use server Directives & Mutations — Next.js 15 & Modern App Router Architecture
- Component Purity: Pure Functions, Idempotence And Side Effects — React 19 & Modern Component Architecture
- State with useState: Immutability And Asynchronous Batching — React 19 & Modern Component Architecture
- Complex State with useReducer: State Machines And Action Types — React 19 & Modern Component Architecture
- SyntheticEvent System: Event Delegation in Modern React — React 19 & Modern Component Architecture
- Avoiding Effect Antipatterns: Derived State And Events — React 19 & Modern Component Architecture
- useRef Mechanics: DOM References And Mutable Persistent State — React 19 & Modern Component Architecture
- Lifting State Up: Colocation, Ownership, And Data Flow — React 19 & Modern Component Architecture
- Compound Components: Inversion of Control with Context — React 19 & Modern Component Architecture
- Higher-Order Components And Render Props: Tradeoff Analysis — React 19 & Modern Component Architecture
- Custom Hook Composition: Encapsulating Reactive Logic — React 19 & Modern Component Architecture