Skip to content
Tutorials Center
My learning

Topics

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.

JSX
<li draggable onDragStart={(e) => e.dataTransfer.setData('text/plain', id)}>
  {label}
</li>

Try it

Move items between two boxes
Which events fire, and in what order

Syntax

JSX
<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() in onDragOver on elements that accept drops. Without it, no drop happens.
  • draggable on non-default elements. Images and links are draggable by default. Other elements need draggable (React writes draggable="true"; the attribute is an enumerated one, so draggable={false} renders draggable="false").
  • onDragLeave also fires when the pointer enters a child of the target, which makes simple "hover" state flicker. Use e.currentTarget.contains(e.relatedTarget) to ignore moves between children, or track a counter.
  • Store ids, not objects. dataTransfer holds strings. Put an id in it and look the object up in state.
  • Dropping files from the desktop uses onDrop with e.dataTransfer.files. Call preventDefault() in both onDragOver and onDrop, 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 draggable element as target.