Mastering Mouse Events in React + TypeScript: Click, Drag, Hover and Beyond
In modern React development, user interactivity is everything. Mouse events form the core of how users engage with your components—from clicking a button, dragging a file, to hovering over content for context.
In this article, we’ll deep dive into the wide array of mouse and drag-related event handlers available in React + TypeScript. We'll explore their purpose, use cases, and demonstrate best practices through an interactive math-based UI.
Why Mouse Events Matter in React
Whether it's clicking a calculator button or dragging a number into a math equation, mouse events allow users to manipulate the UI directly. Mastering these events allows you to build:
- Custom context menus
- Drag-and-drop editors
- Interactive games
- Advanced canvas-based charts
React’s Event System Overview
React uses a synthetic event system—a cross-browser wrapper around the browser’s native event system. It ensures performance and consistency across platforms.
Every HTML element in React supports the following mouse and drag events (typed via MouseEventHandler<T>
or DragEventHandler<T>
):
onClick, onDoubleClick, onMouseDown, onMouseUp,
onMouseEnter, onMouseLeave, onMouseMove, onMouseOver, onMouseOut,
onDrag, onDragStart, onDragEnd, onDragOver, onDrop, onDragEnter, onDragLeave
Complete List of Mouse Event Props
Here’s a categorized snapshot of the available handlers:
Click Events
onClick
onClickCapture
onDoubleClick
onDoubleClickCapture
-
onAuxClick
(middle mouse button)
Drag Events
onDrag
onDragStart
onDragEnd
onDragEnter
onDragLeave
onDragOver
onDrop
onDragExit
Mouse Movement
onMouseDown
onMouseUp
onMouseMove
onMouseEnter
onMouseLeave
onMouseOver
onMouseOut
Real-World Example: Dragging Terms into a Math Equation
Let’s build a component where users drag and drop numbers into an equation area:
import React, { useState } from 'react';
const terms = [2, 4, 6, 8];
export const DragEquation = () => {
const [expression, setExpression] = useState<number[]>([]);
const onDropHandler = (e: React.DragEvent<HTMLDivElement>) => {
const value = parseInt(e.dataTransfer.getData('text'), 10);
setExpression([...expression, value]);
};
const allowDrop = (e: React.DragEvent<HTMLDivElement>) => {
e.preventDefault(); // Must allow drop
};
return (
<div className="p-4">
<h3>🧠 Build the Equation</h3>
<div className="d-flex gap-2 mb-3">
{terms.map((term) => (
<div
key={term}
draggable
onDragStart={(e) => e.dataTransfer.setData('text', term.toString())}
className="px-3 py-2 bg-info text-white rounded"
>
{term}
</div>
))}
</div>
<div
onDrop={onDropHandler}
onDragOver={allowDrop}
className="p-4 border border-primary text-center"
style={{ minHeight: '60px' }}
>
Drop here: {expression.join(' + ') || '---'}
</div>
</div>
);
};
Best Practices for Mouse Events
Tip | Description |
---|---|
✅ Use e.preventDefault()
|
For custom drop zones |
✅ Debounce high-frequency events | Like onMouseMove to improve performance |
✅ Avoid anonymous inline functions | Reuse handlers to avoid re-renders |
✅ Use useCallback with dependencies |
When handlers depend on state |
Conclusion
React gives you full control over mouse interactions through a well-typed and consistent event system. From basic clicks to advanced drag-and-drop, knowing the full spectrum of MouseEventHandler
and DragEventHandler
props allows you to create rich, interactive UIs.
Whether you're building educational tools, games, or data visualization interfaces—mastering mouse events is essential to becoming a React expert.
Tags: react
typescript
events
mouse
dragdrop
frontend
Top comments (0)