Drag and drop basics
Basic drag and drop in React can use the native HTML Drag and Drop API: mark items draggable, store the dragged item's ID in onDragStart (via dataTransfer or state), call preventDefault() in onDragOver to allow dropping, and reorder the array immutably in onDrop. For production, especially touch support, keyboard accessibility and smooth animations, use a library such as dnd kit or Pragmatic drag and drop.
Native API events
onDragStarton the dragged element: sete.dataTransfer.setData("text/plain", id)andeffectAllowed.onDragOveron drop targets:e.preventDefault()is required, or dropping is not allowed.onDragEnter/onDragLeave: highlight drop targets.onDrop: read the data and update state.onDragEnd: clean up visual state whether or not the drop succeeded.
Reordering logic
Remove the dragged item from its index and insert it at the target index, returning a new array. Keep it as a pure function so it is easy to test.
Limitations of the native API
- Poor or no support on touch devices (mobile Safari support is limited).
- Not keyboard accessible by default.
- Limited control over the drag preview and animations.
Accessibility
Provide an alternative: "Move up" and "Move down" buttons, or keyboard drag with Space to pick up and arrow keys to move, plus live-region announcements ("Item moved to position 3"). dnd kit includes keyboard sensors and screen reader announcements.
Libraries
- dnd kit: React-first, sortable lists, sensors for mouse, touch and keyboard.
- Pragmatic drag and drop (Atlassian): small, framework-agnostic, built on the native API.
Code example
1import { useState } from "react";23export function reorder<T>(list: T[], from: number, to: number): T[] {4 const next = [...list];5 const [moved] = next.splice(from, 1);6 next.splice(to, 0, moved);7 return next;8}910type Card = { id: string; title: string };1112export function SortableList({ initial }: { initial: Card[] }) {13 const [cards, setCards] = useState(initial);14 const [dragId, setDragId] = useState<string | null>(null);15 const [overId, setOverId] = useState<string | null>(null);16 const [announcement, setAnnouncement] = useState("");1718 function move(from: number, to: number) {19 if (to < 0 || to >= cards.length || from === to) return;20 setCards((c) => reorder(c, from, to));21 setAnnouncement(`${cards[from].title} moved to position ${to + 1}`);22 }2324 return (25 <>26 <ul>27 {cards.map((card, index) => (28 <li key={card.id} draggable29 onDragStart={(e) => { setDragId(card.id); e.dataTransfer.setData("text/plain", card.id); e.dataTransfer.effectAllowed = "move"; }}30 onDragOver={(e) => { e.preventDefault(); setOverId(card.id); }} // required to allow dropping31 onDrop={(e) => { e.preventDefault(); const from = cards.findIndex((c) => c.id === e.dataTransfer.getData("text/plain")); move(from, index); }}32 onDragEnd={() => { setDragId(null); setOverId(null); }}33 style={{ opacity: dragId === card.id ? 0.4 : 1, borderTop: overId === card.id ? "2px solid #4f46e5" : "2px solid transparent", padding: 8 }}>34 {card.title}35 {/* Keyboard-accessible alternative */}36 <button aria-label={`Move ${card.title} up`} onClick={() => move(index, index - 1)}>↑</button>37 <button aria-label={`Move ${card.title} down`} onClick={() => move(index, index + 1)}>↓</button>38 </li>39 ))}40 </ul>41 <p aria-live="polite" className="sr-only">{announcement}</p>42 </>43 );44}
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.