React Events Deep Dive

Pascual Vila
Senior Frontend Developer // Code Syllabus
React elements handle events similarly to DOM elements, but with crucial syntactic and architectural differences. Understanding these differences is key to building performant apps.
Syntax: camelCase vs lowercase
In traditional HTML, event attributes are written in entirely lowercase letters (e.g., onclick). In React, all event attributes are written in camelCase (e.g., onClick, onMouseEnter, onChange).
Furthermore, in HTML you pass a string as the event handler. In React, you pass a function reference inside JSX curly braces.
Passing Arguments to Handlers
A common mistake beginners make is invoking the function immediately upon rendering: onClick={handleDelete(id)}. This causes an infinite loop or immediate execution. Instead, use an inline arrow function: onClick={() => handleDelete(id)}. This creates a new function that waits for the click to execute.
Synthetic Events
React implements a cross-browser wrapper around the native browser events called SyntheticEvent. This wrapper has the exact same interface as the browser's native event (including stopPropagation() and preventDefault()), ensuring that events work identically across all browsers.
Because of this architecture, you cannot return false to prevent default behavior in React. You must explicitly call e.preventDefault().