Handle clicks
Buttons and controls need typed click handling.
React 19+ · Type Alias
MouseEvent is React’s handler type for mouse interactions on built-in elements. Its event object exposes button state, modifier keys, coordinates, movement, and the related target.
In React, MouseEvent is the event type used by mouse-related handler props such as onClick, onMouseDown, and onMouseMove. The event object exposes modifier-key state, button information, coordinates, movement data, and the related target, so a typed handler can inspect pointer input without losing React’s event typing.
(e: React.MouseEvent<HTMLButtonElement>) => void| Name | Type | Description |
|---|---|---|
e | React.MouseEvent | The React mouse event object supplied to the handler; it exposes mouse-specific properties such as coordinates, buttons, modifier keys, movement, and relatedTarget. |
The handler returns nothing required by the documentation; it performs the event response.
export default function Button() {
function handleClick(event: React.MouseEvent<HTMLButtonElement>) {
if (event.button === 0) {
console.log(event.clientX, event.clientY);
}
}
return <button onClick={handleClick}>Click</button>;
}The handler receives a React mouse event and reads button and viewport-coordinate data.
Buttons and controls need typed click handling.
A component must position UI from pointer coordinates.
Interactions depend on mouse buttons or keyboard modifiers.
MouseEvent is the React handler type for mouse events. It is used with props such as onClick, onMouseDown, onMouseUp, onMouseOver, and onMouseLeave, and its event object includes coordinates, modifier keys, button state, and relatedTarget.
Follow-up: How would you distinguish a primary-button click? Check the event’s button property and handle the primary-button value rather than assuming every click came from the same button.
The event object provides mouse-specific fields including clientX and clientY for viewport coordinates, pageX and pageY for page coordinates, movementX and movementY for movement, and altKey, ctrlKey, metaKey, and shiftKey for modifiers.
Follow-up: Which coordinate pair is appropriate for viewport-relative positioning? Use clientX and clientY; use pageX and pageY when document scrolling should be included.
onMouseEnter and onMouseLeave are mouse-event handlers, but React documents that they do not have a capture phase. Instead, these events propagate from the element being left to the element being entered.
Follow-up: Why should hover behavior not automatically be implemented with capture handlers? Because these two events have their own propagation model and do not expose a capture phase.
onAuxClickonClickonDoubleClickonMouseDownonMouseEnteronMouseLeaveonMouseMoveonMouseOver