Skip to content
Back to Pathway
fundamentalsbeginner

Event Handling

React wraps native browser events in SyntheticEvent objects for cross-browser consistency and unified delegation.

Mental Model

"The Universal Adapter: No matter what country outlet you plug into, the adapter gives your laptop clean standard power."

Interactive Engine Walkthrough

Synthetic Events
#root container1 native listener lives here
<Card> — click this area for target=Card
handler wiring
elementonClickstop
RootContainer
Card
PunchButton
onClick props 3 native listeners 1
bubble pathclick something inside the tree to fire a real event…

Add five buttons and the onClick-prop count climbs — the listener count stays at 1. React never touches each button: one root listener dispatches the synthetic event down the path you just watched.

Reference Code Snippet

Component.jsx
JSX / React 19
1function Dashboard() {
2  const handleClick = (event) => {
3    // This is a SyntheticEvent, not a NativeEvent
4    console.log(event.type); 
5    event.stopPropagation();
6  };
7
8  return <button onClick={handleClick}>Execute</button>;
9}
Technical Takeaways & Best Practices
  • 01.React event names use camelCase (onClick, onSubmit, onKeyDown) instead of lowercase.
  • 02.Event handlers are passed as function references: onClick={handleClick}, NOT onClick={handleClick()}.
  • 03.React delegates event listeners to the root container rather than attaching individual listeners to every DOM node.

01. Synthetic Events

React doesn't attach events directly to the elements you define. Instead, it wraps native browser events in a **SyntheticEvent** object. This ensures identical behavior across all browsers (Chrome, Safari, Firefox) and improves performance through event delegation.

Modern Delegation (React 17+)

Browser Event
→
App Root Container
→
Your Handler

02. Automatic Binding

In modern function components, you don't need to worry about `this` binding. However, remember that React handlers are passed as **camelCase** (`onClick` instead of `onclick`). These handlers are executed during the Bubble phase by default.

Expert Note

"Because React uses delegation at the root, calling 'stopPropagation' doesn't stop the native event from reaching the document. It only stops it within the React virtual tree. This is a common source of confusion when mixing React with legacy jQuery plugins."