Rather than attaching a separate native event listener to every individual DOM node the way vanilla JavaScript typically does, React attaches a single listener at the application's root and uses event delegation, catching events as they bubble up and routing them internally to the correct component's handler based on where the event actually originated. Every handler receives a SyntheticEvent object, a cross-browser wrapper around the native event that normalizes behavior and exposes the same familiar properties and methods, like .target and .preventDefault(), consistently regardless of which underlying browser is running the code.
1Understanding Events in React
Rather than attaching a separate native event listener to every individual DOM node the way vanilla JavaScript typically does, React attaches a single listener at the application's root and uses event delegation, catching events as they bubble up and routing them internally to the correct component's handler based on where the event actually originated. Every handler receives a SyntheticEvent object, a cross-browser wrapper around the native event that normalizes behavior and exposes the same familiar properties and methods, like .target and .preventDefault(), consistently regardless of which underlying browser is running the code.
Because of event delegation, you generally don't need to worry about attaching or removing individual DOM event listeners yourself in React — just declaring onClick={handler} in JSX handles all of that internally.
function Button() {
const handleClick = (event) => {
console.log('Clicked element:', event.target.tagName);
};
return <button onClick={handleClick}>Click me</button>;
}2Practical Example
Here is a real-world application of Events in React showing how it is used in production React code.
function Link() {
const handleClick = (event) => {
event.preventDefault();
console.log('Default navigation prevented');
};
return <a href="https://example.com" onClick={handleClick}>Go</a>;
}3Best Practices
Follow these guidelines when working with Events in React:
1. Attach event handlers declaratively via JSX props, like onClick={handler}, rather than manually calling addEventListener on refs, unless you specifically need to listen to an event React doesn't support directly
2. Rely on the SyntheticEvent object's normalized properties and methods, like .preventDefault(), for consistent cross-browser behavior rather than reaching for browser-specific native event quirks
3. Remember JSX event names are camelCase, like onClick and onChange, not the lowercase onclick/onchange used in plain HTML attributes
Tip: Because of event delegation, you generally don't need to worry about attaching or removing individual DOM event listeners yourself in React — just declaring onClick={handler} in JSX handles all of that internally.
function Button() {
const handleClick = (event) => {
console.log('Clicked element:', event.target.tagName);
};
return <button onClick={handleClick}>Click me</button>;
}