How to Build a React Drag and Drop Component (dnd-kit + HTML5)

Our web team at a Denver outdoor store runs the holiday launch from a three-column task board, and everyone wanted a React drag and drop component to move the cards.

Quick answer: the simplest solid React drag and drop component uses dnd-kit. Wrap the board in DndContext, make cards useDraggable and columns useDroppable, then update state in onDragEnd; with no library, use HTML5 draggable with onDragOver and onDrop.

<DndContext onDragEnd={({ active, over }) => moveTask(active.id, over?.id)}>
  <Column id="doing">{cards}</Column>
</DndContext>

In this tutorial, we’ll build a React drag and drop component three ways on one task board: the HTML5 API, dnd-kit drop zones and a sortable list you can reorder by mouse or keyboard.

What You Need Before You Start a React Drag and Drop Board

You need Node.js and a React + TypeScript app; setting up a React environment covers it. Steps 2 to 4 add three dnd-kit packages:

npm create vite@latest task-board -- --template react-ts
cd task-board
npm install @dnd-kit/core @dnd-kit/sortable @dnd-kit/utilities

We tested on React 19.3, @dnd-kit/core 6.3.1, @dnd-kit/sortable 10.0.0, TypeScript 7.0.2 and Vite 8.3. Every browser shot is a real drag in Microsoft Edge, and every file passed strict tsc. If the prop types look odd, see React component types in TypeScript.

Save the five sample launch tasks as src/tasks.ts:

export type Status = "todo" | "doing" | "done";

export interface Task {
  id: string;
  title: string;
  owner: string;
  status: Status;
}

export const columns: { id: Status; title: string }[] = [
  { id: "todo", title: "To Do" },
  { id: "doing", title: "In Progress" },
  { id: "done", title: "Done" },
];

export const tasks: Task[] = [
  { id: "t1", title: "Fix sales tax on checkout", owner: "Megan",
    status: "todo" },
  { id: "t2", title: "Write Black Friday email", owner: "Jordan",
    status: "todo" },
  { id: "t3", title: "Update shipping rates", owner: "Priya",
    status: "doing" },
  { id: "t4", title: "Test the mobile menu", owner: "Carlos",
    status: "doing" },
  { id: "t5", title: "Publish the gift guide", owner: "Hannah",
    status: "done" },
];

Style src/board.css however you like, but give cards touch-action: none so phones don’t scroll mid-drag.

Step 1: Build a Drag and Drop Board with the HTML5 API

Browsers have drag and drop built in. Cards get draggable, and three React event handlers do the rest: onDragStart stores the id, onDragOver allows the drop, onDrop moves the task.

import { useState } from "react";
import type { DragEvent } from "react";
import { columns, tasks as start } from "./tasks";
import type { Status, Task } from "./tasks";
import "./board.css";

export default function App() {
  const [items, setItems] = useState<Task[]>(start);

  const onDragStart = (e: DragEvent<HTMLDivElement>, id: string) => {
    e.dataTransfer.setData("text/plain", id);
    e.dataTransfer.effectAllowed = "move";
    console.log("drag start", id);
  };

  const onDrop = (e: DragEvent<HTMLElement>, status: Status) => {
    e.preventDefault();
    const id = e.dataTransfer.getData("text/plain");
    setItems((prev) =>
      prev.map((t) => (t.id === id ? { ...t, status } : t)));
    console.log("dropped", id, "in", status);
  };

  return (
    <div className="board">
      {columns.map((col) => (
        <section key={col.id} id={col.id} className="column"
          onDragOver={(e) => e.preventDefault()}
          onDrop={(e) => onDrop(e, col.id)}>
          <h2>{col.title}</h2>
          {items.filter((t) => t.status === col.id).map((t) => (
            <div key={t.id} id={t.id} className="card" draggable
              onDragStart={(e) => onDragStart(e, t.id)}>
              {t.title} <small>{t.owner}</small>
            </div>
          ))}
        </section>
      ))}
    </div>
  );
}

Console output after dragging “Fix sales tax on checkout” into In Progress:

drag start t1
dropped t1 in doing
React HTML5 drag and drop board after moving Fix sales tax on checkout from To Do to In Progress
Megan’s tax fix now sits in In Progress.

dataTransfer only carries strings, so we pass the id. The map returns a new array, which is what makes React re-render.

VS Code with the HTML5 drag and drop App.tsx open and tsc plus the Vite dev server running in the terminal
The same file in VS Code: tsc passes and Vite serves it on port 5173.

Note: the HTML5 API has no keyboard support and no built-in reordering. That’s where a library helps.

Step 2: Move Cards Between Columns with dnd-kit

Same board, now on dnd-kit. useDraggable gives a card a ref, listeners and a live transform; useDroppable makes a column a drop zone that gets its cards as children props.

import { useState } from "react";
import type { ReactNode } from "react";
import { DndContext, useDraggable, useDroppable } from "@dnd-kit/core";
import type { DragEndEvent } from "@dnd-kit/core";
import { CSS } from "@dnd-kit/utilities";
// + the tasks.ts and board.css imports from Step 1

function Card({ task }: { task: Task }) {
  const { attributes, listeners, setNodeRef, transform } =
    useDraggable({ id: task.id });
  return (
    <div ref={setNodeRef} id={task.id} className="card"
      style={{ transform: CSS.Translate.toString(transform) }}
      {...listeners} {...attributes}>
      {task.title} <small>{task.owner}</small>
    </div>
  );
}

type ColumnProps = { id: Status; title: string; children: ReactNode };

function Column({ id, title, children }: ColumnProps) {
  const { setNodeRef, isOver } = useDroppable({ id });
  return (
    <section ref={setNodeRef} id={id}
      className={isOver ? "column over" : "column"}>
      <h2>{title}</h2>
      {children}
    </section>
  );
}

export default function App() {
  const [items, setItems] = useState<Task[]>(start);

  function handleDragEnd({ active, over }: DragEndEvent) {
    if (!over) return; // dropped outside every column
    const status = over.id as Status;
    setItems((prev) =>
      prev.map((t) => (t.id === active.id ? { ...t, status } : t)));
    console.log(active.id, "->", status);
  }

  return (
    <DndContext onDragEnd={handleDragEnd}>
      <div className="board">
        {columns.map((col) => (
          <Column key={col.id} id={col.id} title={col.title}>
            {items.filter((t) => t.status === col.id)
              .map((t) => <Card key={t.id} task={t} />)}
          </Column>
        ))}
      </div>
    </DndContext>
  );
}

Console output after dragging “Write Black Friday email” into Done:

t2 -> done
dnd-kit drag in progress: the Write Black Friday email card follows the pointer over the Done column, which shows a blue dashed border
Mid-drag, isOver gives Done a blue dashed border.
React dnd-kit task board after dropping Write Black Friday email in the Done column
After the drop, Jordan’s email is in Done.

over is null outside every column, so the card stays put. To move one box freely with no drop zones, make a component draggable in React instead.

Step 3: Reorder a List with dnd-kit Sortable

Next, the team wanted a sortable priority list. @dnd-kit/sortable adds SortableContext, useSortable and arrayMove, which returns a reordered copy.

import { useState } from "react";
import {
  DndContext, KeyboardSensor, PointerSensor, closestCenter,
  useSensor, useSensors,
} from "@dnd-kit/core";
import type { DragEndEvent } from "@dnd-kit/core";
import {
  SortableContext, arrayMove, sortableKeyboardCoordinates,
  useSortable, verticalListSortingStrategy,
} from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";
// + the tasks.ts and board.css imports from Step 1

function Row({ task, rank }: { task: Task; rank: number }) {
  const { attributes, listeners, setNodeRef, transform, transition,
    isDragging } = useSortable({ id: task.id });
  return (
    <li ref={setNodeRef} id={task.id} className="card row"
      style={{
        transform: CSS.Transform.toString(transform),
        transition,
        opacity: isDragging ? 0.6 : 1,
      }}
      {...attributes} {...listeners}>
      <b>{rank}</b><span>{task.title}</span><small>{task.owner}</small>
    </li>
  );
}

export default function App() {
  const [items, setItems] = useState<Task[]>(start);
  const sensors = /* pointer + keyboard sensors, see Step 4 */;

  function handleDragEnd({ active, over }: DragEndEvent) {
    if (!over || active.id === over.id) return;
    const from = items.findIndex((t) => t.id === active.id);
    const to = items.findIndex((t) => t.id === over.id);
    const next = arrayMove(items, from, to);
    setItems(next);
    console.log("new order:", next.map((t) => t.id).join(", "));
  }

  return (
    <DndContext sensors={sensors} collisionDetection={closestCenter}
      onDragEnd={handleDragEnd}>
      <SortableContext items={items}
        strategy={verticalListSortingStrategy}>
        <div className="list">
          <h2>Launch priorities</h2>
          <ol style={{ padding: 0 }}>
            {items.map((t, i) => (
              <Row key={t.id} task={t} rank={i + 1} />
            ))}
          </ol>
        </div>
      </SortableContext>
    </DndContext>
  );
}

Console output after dragging “Publish the gift guide” to the top:

new order: t5, t1, t2, t3, t4
React dnd-kit sortable list after dragging Publish the gift guide from position 5 to position 1
The gift guide jumped from fifth to first.

closestCenter picks the row nearest the dragged item, which suits vertical lists. Ranks come from the array index, so they stay correct.

VS Code with the dnd-kit sortable App.tsx open and the terminal showing tsc and the Vite dev server
The sortable App.tsx in VS Code with no tsc errors.

Step 4: Add Keyboard Support and Click-Safe Dragging

PointerSensor with distance: 5 waits for a 5-pixel move, so plain clicks still work. Keyb