Introduction
React lets you respond to user interactions like clicks, key presses, and form changes by attaching event handlers directly to JSX elements. Instead of raw DOM events, React wraps browser events in a cross-browser wrapper called a SyntheticEvent, which normalizes behavior across different browsers and works consistently with React's event delegation system. In React 17+, events are attached at the root DOM container instead of document, but the SyntheticEvent API you write against stays the same.
Cricket analogy: Attaching onClick to a JSX button is like a fielding coach responding to a catch signal from anywhere on the field; React wraps the raw signal in a SyntheticEvent, a standardized umpire signal that means the same thing in Mumbai or Melbourne, and since React 17 it's relayed through the stadium's central hub rather than the league office.
Syntax
function Button() {
function handleClick(event) {
console.log('Button clicked!', event.type);
}
return <button onClick={handleClick}>Click me</button>;
}Explanation
React event handler props use camelCase (onClick, onChange, onSubmit) rather than lowercase HTML attributes (onclick). You pass a function reference, not a string, and you should not call the function immediately — write onClick={handleClick}, never onClick={handleClick()}, since the latter invokes it during render. The handler receives a SyntheticEvent object as its argument, which has the same interface as native events (target, preventDefault, stopPropagation) but is pooled for performance in older React versions and safe to read asynchronously in React 17+. To pass extra arguments, wrap the call in an arrow function, e.g. onClick={() => handleClick(id)}.
Cricket analogy: Naming a handler onClick, camelCase not 'onclick', and passing onClick={handleAppeal} — never handleAppeal(), which would fire the appeal instantly during rendering instead of waiting for the umpire's real signal; to pass which fielder is appealing, wrap it as onClick={() => handleAppeal(fielderId)}.
Example
function TodoItem({ id, text, onRemove }) {
function handleRemoveClick(event) {
event.stopPropagation();
onRemove(id);
}
return (
<li onClick={() => console.log('Row clicked', id)}>
{text}
<button onClick={handleRemoveClick}>Delete</button>
</li>
);
}Output
Clicking anywhere on the list item logs 'Row clicked' with the item's id. Clicking the Delete button calls stopPropagation() first, so the row's onClick does not also fire, and then calls onRemove(id) to notify the parent component that the item should be removed from state.
Cricket analogy: Clicking anywhere on a player's row logs 'Row clicked' with their id, but clicking 'Drop from XI' calls stopPropagation() first so the row click doesn't also fire, then calls onRemove(id) to notify the selection committee's parent state.
Key Takeaways
- React event props use camelCase, e.g. onClick, onChange, onSubmit.
- Pass a function reference to the handler prop; use an arrow function to pass arguments.
- SyntheticEvent normalizes native events across browsers and exposes .preventDefault() and .stopPropagation().
- Since React 17, events are delegated to the root container instead of document, easing multi-version React embedding.
- Avoid inline function creation in performance-critical lists when it causes unnecessary re-renders of memoized children.
Practice what you learned
1. How should an onClick handler be attached to a button in JSX?
2. What is a SyntheticEvent in React?
3. How do you pass an argument to an event handler beyond the event object itself?
4. Where does React attach event listeners as of React 17+?
Was this page helpful?
You May Also Like
Forms in React
Understand controlled and uncontrolled form inputs and how to manage form state in React.
Form Validation in React
Implement client-side validation logic for React forms, including error state and submit-time checks.
The useState Hook
Master useState, the Hook that adds local state to function components, including initial values, updater functions, and functional updates.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentSpring Boot Study Notes
Java · 30 topics
Web DevelopmentFlask Study Notes
Python · 30 topics
Web DevelopmentDjango Study Notes
Python · 30 topics