React Events & Delegation
Handling events in React is very similar to handling events on DOM elements, but with a few key syntactic differences and a powerful normalization layer.
Synthetic Events
When an event fires in React, your handler is passed a SyntheticEvent. This is a cross-browser wrapper around the browser's native event. It has the same interface as the browser's native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers.
camelCase Syntax
In standard HTML, you might write onclick="handleClick()". In React, events are named using camelCase, and you pass a function reference rather than a string: onClick={handleClick}.
Event Delegation
React doesn't actually attach event handlers to the nodes themselves. Instead, React attaches one universal event listener to the root of the document. When an event fires, React maps it to the appropriate component and dispatches the SyntheticEvent. This improves performance dramatically on large lists or tables!
View Full Transcript+
This section covers the theory behind Event Delegation. By managing a single event listener at the root of the React application, memory usage is kept incredibly low. Additionally, the concept of Event Pooling (used in React 16 and older) meant the SyntheticEvent object was reused for performance, nullifying properties after the callback. Note: React 17+ dropped Event Pooling for better predictability, but retains delegation!
