Note Pass a function reference, not a function call. Writing onClick={handler()} would call the function during render. Use an arrow function when you need to pass arguments: onClick={() => handler(id)}.
Note React's onChange fires on every keystroke, not just when the field loses focus (which is the native DOM behavior). This makes it ideal for real-time validation and derived values.
Note Always call e.preventDefault() in onSubmit to prevent the browser from performing a full page reload. The submit event fires when pressing Enter inside an input or clicking a submit button.
functionhandler(event){
event.target// the DOM element
event.currentTarget// element the handler is attached to
event.preventDefault()
event.stopPropagation()
event.nativeEvent// underlying browser event}
Example
functionLinkInterceptor({ children }){functionhandleClick(e){// Check if it's an internal linkconst href = e.target.closest('a')?.getAttribute('href');if(href?.startsWith('/')){
e.preventDefault();navigate(href);// client-side navigation}}return<div onClick={handleClick}>{children}</div>;}
Note React wraps native events in SyntheticEvent objects that work identically across browsers. As of React 17+, events are no longer pooled, so you can access event properties asynchronously without calling e.persist().
functionDropZone({ onFileDrop }){functionhandleDragOver(e){
e.preventDefault();// Required to allow drop}functionhandleDrop(e){
e.preventDefault();
e.stopPropagation();const files =Array.from(e.dataTransfer.files);onFileDrop(files);}return(<div
onDragOver={handleDragOver}
onDrop={handleDrop}
className="drop-zone">Drop files here
</div>);}
Note Use preventDefault for links, form submissions, and drag-and-drop. Use stopPropagation sparingly -- it makes debugging harder. Prefer checking the event target to decide whether to act instead of stopping propagation.
preventDefaultstopPropagationprevent defaultstop bubblingdrag and drop
Capture Phase & Delegation
Syntax
// Capture phase (fires before children)<div onClickCapture={handler}>...</div>// Event delegation: one handler on parent<ul onClick={handleItemClick}><li data-id="1">A</li><li data-id="2">B</li></ul>
Note Event delegation (one handler on a parent) reduces the number of event listeners and works well for dynamic lists. Use data-* attributes or closest() to identify which child triggered the event. React internally uses delegation at the root already.