React Reference ยท Props and Attributes
React drag events (onDragStart, onDrop and dataTransfer)
Reference for React drag and drop events: draggable, onDragStart, onDragOver, onDrop, dataTransfer, effectAllowed and why onDragOver must call preventDefault.
React exposes the browser's HTML drag and drop API as a family of props. An element becomes a drag source with the draggable attribute and onDragStart. Another element becomes a drop target with onDragOver and onDrop. Between the two, a dataTransfer object carries a small payload, usually the id of the thing being moved.
The API is old and has a few traps. The biggest one: a drop target must call event.preventDefault() in onDragOver, otherwise the browser refuses the drop and onDrop never fires. This page lists the events, the order they fire in, and the pattern that works.
<li draggable onDragStart={(e) => e.dataTransfer.setData('text/plain', id)}> {label} </li>
Try it
Syntax
<element draggable onDragStart onDrag onDragEnd onDragEnter onDragOver onDragLeave onDrop /> // each handler: (event: React.DragEvent) => void
| Event | Fires on | When |
|---|---|---|
onDragStart |
the dragged element | the drag begins |
onDrag |
the dragged element | continuously, every few hundred ms, while dragging |
onDragEnd |
the dragged element | the drag stops, dropped or cancelled |
onDragEnter |
a potential target | the pointer enters it |
onDragOver |
a potential target | continuously while the pointer is over it |
onDragLeave |
a potential target | the pointer leaves it |
onDrop |
the target | the user releases over a target that allowed it |
event.dataTransfer offers setData(type, text), getData(type), effectAllowed (set in onDragStart), dropEffect (set in onDragOver), files (dropped files) and setDragImage().
Errors and edge cases: getData only returns data during onDrop; in onDragOver and onDragEnter the data is hidden for security, though dataTransfer.types lists the formats.
Usage notes
- Always
preventDefault()inonDragOveron elements that accept drops. Without it, no drop happens. draggableon non-default elements. Images and links are draggable by default. Other elements needdraggable(React writesdraggable="true"; the attribute is an enumerated one, sodraggable={false}rendersdraggable="false").onDragLeavealso fires when the pointer enters a child of the target, which makes simple "hover" state flicker. Usee.currentTarget.contains(e.relatedTarget)to ignore moves between children, or track a counter.- Store ids, not objects.
dataTransferholds strings. Put an id in it and look the object up in state. - Dropping files from the desktop uses
onDropwithe.dataTransfer.files. CallpreventDefault()in bothonDragOverandonDrop, or the browser will open the file. - No touch support. HTML drag and drop does not fire from touch on most mobile browsers. For touch, use pointer events or a library.
- Keyboard users. Drag and drop is not accessible by itself. Offer buttons such as "Move to Done" as an alternative.
- Dragging text selections triggers drag events too; they have no
draggableelement as target.