How to Build a Kanban Board in React That Moves Cards Between Columns
With dnd-kit, put a SortableContext in each column and use onDragOver, which runs during a drag, to move a card into another column's array. This lets you build a kanban board where cards move between columns. It fits a task view where you drag and drop cards between the "To Do", "Doing", and "Done" columns to change their status.
In my test environment, I built a board with three columns and confirmed that cards could be moved to other columns with both the mouse and the keyboard. I also cover the case where a column with no cards can't accept a drop, and how to fix it.
The test environment was Ubuntu 24.04 and Node.js 22.23.4. I used react / react-dom 19.3.1, @dnd-kit/core 6.3.1, @dnd-kit/sortable 10.0.0, @dnd-kit/utilities 3.2.2, @types/react 19.3.0, typescript 7.0.2, and esbuild 0.28.2. I type-checked the .tsx files with tsc, built for production with esbuild, and ran them in Chromium 153 (headless, operated with Playwright 1.63.0). I did not wrap the app in StrictMode.
This article does not cover touch operation on phones and other touch devices, reordering the columns themselves, saving the board contents, or screen reader announcements. I did not test these.
- 1. Store the board data as an array of cards per column
- 2. The card and column components
- 3. Use onDragOver to move a card to another column during the drag
- 4. Results of the drags
- 5. Moving only on drop leaves no gap at the destination
- 6. To drop into an empty column, make the column a useDroppable
- 7. Draw the grabbed card outside the column with DragOverlay
- 8. You can also move cards between columns with the keyboard
- 9. When TypeScript reports errors in the dnd-kit type definitions
Store the board data as an array of cards per column
I used three columns: "To Do", "Doing", and "Done". The board state is an object keyed by the column id, where each value is an array of cards. The order of the cards is the order of the array.
export type ColumnId = 'todo' | 'doing' | 'done';
export type Card = { id: string; title: string };
export type Board = Record<ColumnId, Card[]>;
export const columnTitles: Record<ColumnId, string> = {
todo: '未着手',
doing: '作業中',
done: '完了',
};
export const columnIds = Object.keys(columnTitles) as ColumnId[];
export const initialBoard: Board = {
todo: [
{ id: 'c1', title: '見積もりを作る' },
{ id: 'c2', title: '画像を差し替える' },
{ id: 'c3', title: '議事録をまとめる' },
],
doing: [{ id: 'c4', title: 'ログイン画面を直す' }],
done: [],
};
In drag events, you use the id of the grabbed card (active.id) and the id of whatever is under it, either a card or a column (over.id). So I put two functions in the same file: one that finds which column an id belongs to, and one that moves a card to another column.
// Find which column the id belongs to (the column if it's a column id, or the column containing the card if it's a card id)
export function findColumn(board: Board, id: string): ColumnId | undefined {
if (id in board) return id as ColumnId;
return columnIds.find((col) => board[col].some((card) => card.id === id));
}
// Move the card to another column (to that position if overId is a card, or to the end of that column if it's a column)
export function moveToColumn(board: Board, activeId: string, from: ColumnId, to: ColumnId, overId: string): Board {
const card = board[from].find((c) => c.id === activeId)!;
const overIndex = board[to].findIndex((c) => c.id === overId);
const index = overIndex === -1 ? board[to].length : overIndex;
return {
...board,
[from]: board[from].filter((c) => c.id !== activeId),
[to]: [...board[to].slice(0, index), card, ...board[to].slice(index)],
};
}
The card and column components
Install these three packages.
npm install @dnd-kit/core @dnd-kit/sortable @dnd-kit/utilities
Each card is made with useSortable. I made the whole card grabbable.
import { useSortable } from '@dnd-kit/sortable';
import { CSS } from '@dnd-kit/utilities';
import type { Card } from './board';
export function SortableCard({ card }: { card: Card }) {
const { attributes, listeners, setNodeRef, transform, transition, isDragging } =
useSortable({ id: card.id });
const style = {
transform: CSS.Transform.toString(transform),
transition,
// While dragging, leave only a faint shadow in the original spot
opacity: isDragging ? 0.3 : 1,
};
return (
<li ref={setNodeRef} style={style} className="card" data-id={card.id} {...attributes} {...listeners}>
{card.title}
</li>
);
}
In the column component, I wrap the list of cards in a SortableContext for each column. According to the README of @dnd-kit/sortable, useSortable combines useDraggable and useDroppable. SortableContext accepts a string id (the type definition has id?: string). Here I passed the column id.
import { useDroppable } from '@dnd-kit/core';
import { SortableContext, verticalListSortingStrategy } from '@dnd-kit/sortable';
import { columnTitles, type Card, type ColumnId } from './board';
import { SortableCard } from './SortableCard';
export function Column({ id, cards }: { id: ColumnId; cards: Card[] }) {
// Make the column itself a drop target too (so a card can be dropped into a column with no cards)
const { setNodeRef } = useDroppable({ id });
return (
<section className="column" data-column={id}>
<h2>
{columnTitles[id]}({cards.length})
</h2>
<SortableContext id={id} items={cards} strategy={verticalListSortingStrategy}>
<ul ref={setNodeRef} className="cards">
{cards.map((card) => (
<SortableCard key={card.id} card={card} />
))}
</ul>
</SortableContext>
</section>
);
}
useDroppable makes the ul that holds the cards a drop target. Without it, I could not drop a card into a column that had no cards. I describe the result in a later section. I gave the ul
min-height: 120px in CSS so that empty columns still have some height.
Use onDragOver to move a card to another column during the drag
This is the component for the whole board. I pass the start, middle, and end handlers of the drag to DndContext.
import { useState } from 'react';
import {
DndContext,
DragOverlay,
closestCorners,
KeyboardSensor,
PointerSensor,
useSensor,
useSensors,
type DragEndEvent,
type DragOverEvent,
type DragStartEvent,
} from '@dnd-kit/core';
import { arrayMove, sortableKeyboardCoordinates } from '@dnd-kit/sortable';
import { columnIds, findColumn, initialBoard, moveToColumn, type Board, type Card } from './board';
import { Column } from './Column';
export function KanbanBoard() {
const [board, setBoard] = useState<Board>(initialBoard);
const [activeCard, setActiveCard] = useState<Card | null>(null);
const sensors = useSensors(
useSensor(PointerSensor, { activationConstraint: { distance: 5 } }),
useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates }),
);
// Remember the grabbed card (to draw it in DragOverlay)
function handleDragStart({ active }: DragStartEvent) {
const col = findColumn(board, String(active.id));
setActiveCard(col ? board[col].find((c) => c.id === active.id) ?? null : null);
}
// When dragging enters another column, move the card there right away
function handleDragOver({ active, over }: DragOverEvent) {
if (!over) return;
const activeId = String(active.id);
const overId = String(over.id);
setBoard((prev) => {
const from = findColumn(prev, activeId);
const to = findColumn(prev, overId);
if (!from || !to || from === to) return prev;
return moveToColumn(prev, activeId, from, to, overId);
});
}
// On release, confirm the reordering within the same column
function handleDragEnd({ active, over }: DragEndEvent) {
setActiveCard(null);
if (!over) return;
const activeId = String(active.id);
const overId = String(over.id);
setBoard((prev) => {
const col = findColumn(prev, activeId);
if (!col || col !== findColumn(prev, overId)) return prev;
const oldIndex = prev[col].findIndex((c) => c.id === activeId);
const newIndex = prev[col].findIndex((c) => c.id === overId);
if (newIndex === -1 || oldIndex === newIndex) return prev;
return { ...prev, [col]: arrayMove(prev[col], oldIndex, newIndex) };
});
}
return (
<DndContext
sensors={sensors}
collisionDetection={closestCorners}
onDragStart={handleDragStart}
onDragOver={handleDragOver}
onDragEnd={handleDragEnd}
onDragCancel={() => setActiveCard(null)}
>
<div className="board">
{columnIds.map((id) => (
<Column key={id} id={id} cards={board[id]} />
))}
</div>
<DragOverlay>
{activeCard ? <div className="card overlay">{activeCard.title}</div> : null}
</DragOverlay>
</DndContext>
);
}
The key is the division of work between handleDragOver and handleDragEnd. Moving a card between columns happens in handleDragOver, which runs during the drag. When the card moves over a card or a column in another column, the state array is rewritten at that moment. So before you release the mouse, the card is already in the column it is moving to. handleDragEnd only confirms a reorder within the same column, using arrayMove, when you release.
For collision detection (collisionDetection), I used closestCorners. Its type definition comment describes it as returning the rectangle, from a list of rectangles, that is closest to the corners of a given rectangle. The distance: 5 I passed to PointerSensor's activationConstraint means the drag starts once the pointer has moved more than 5px after pressing. I confirmed this in the code of @dnd-kit/core 6.3.1 distributed on npm.
Results of the drags
In Chromium, I dragged cards three times using the mouse. The state below lists the cards in each column from top to bottom.
最初: 未着手: 見積もりを作る, 画像を差し替える, 議事録をまとめる | 作業中: ログイン画面を直す | 完了: (なし)
1. 「見積もりを作る」を作業中の列の下のほうへ
→ 作業中: ログイン画面を直す, 見積もりを作る
2. 「画像を差し替える」を空の完了の列へ
→ 完了: 画像を差し替える
3. 作業中の列で「ログイン画面を直す」を1つ下へ
→ 作業中: 見積もりを作る, ログイン画面を直す
最後: 未着手: 議事録をまとめる | 作業中: 見積もりを作る, ログイン画面を直す | 完了: 画像を差し替える
Moving between columns, moving into a column with no cards, and reordering within the same column all worked as I expected. I made a GIF of the first and second drags.

The box at the bottom is a display added by my capture script. While the mouse button is held, the cursor ring is pink. The mouse state text is pink at the moment of pressing and at the moment of releasing. When a column's card count changes, that column's count is pink.
During the second drag, on the way to the Done column, the card count of the Doing column rose to 3 for a moment. As the card passed over the Doing column on its path, onDragOver moved it there, so it briefly entered that column. When it moved up over the Done column, it moved again into the Done column. When I released it, the card was in the Done column.
Moving only on drop leaves no gap at the destination
I also tried a version without onDragOver, where the move between columns happens inside onDragEnd. The result after release was the same. The difference was during the drag. I compared the state just before release using the same first drag. In the modified onDragEnd used for the comparison, I moved the card with this one line when the columns differed (from and to are the columns found with findColumn).
if (from !== to) return moveToColumn(prev, activeId, from, to, overId);
The state just before release was as follows.
onDragOver あり: 未着手: 画像を差し替える, 議事録をまとめる | 作業中: ログイン画面を直す, 見積もりを作る
onDragOver なし: 未着手: 見積もりを作る, 画像を差し替える, 議事録をまとめる | 作業中: ログイン画面を直す
Without onDragOver, the card stays in its original column's array until you release it. In that case, the transform of "Fix the login screen" in the Doing column was none, and nothing in the Doing column changed. With onDragOver, the faded card appears at the position it will move to before release, as described in the DragOverlay section below.
To drop into an empty column, make the column a useDroppable
I disabled the column's useDroppable with disabled: true and did the same three drags. In the second drag, "Replace the image" was released over the empty Done column, but it went to the top of the Doing column instead of Done.
2. 「画像を差し替える」を空の完了の列へ
→ 作業中: 画像を差し替える, ログイン画面を直す, 見積もりを作る | 完了: (なし)
useSortable uses useDroppable internally, so each individual card is a drop target. An empty column has no cards, so without the column's own useDroppable there is no drop target at all inside it. In the finished version, where the ul of the column is a useDroppable, the same drag put the card into the Done column. In this case findColumn returns the Done column only when over.id is the column's id ('done'). moveToColumn puts the card at the end of the column when over.id is the column's id.
To allow drops into a column with no cards, make the column element a useDroppable so that its column id comes through as over.id.
Draw the grabbed card outside the column with DragOverlay
Inside DragOverlay, I draw an element that looks like the grabbed card. The dnd-kit README explains that DragOverlay renders an element outside the normal document flow, positioned relative to the viewport (the browser's visible area).
During the first drag, I checked the element for "Make an estimate". The mouse position was (380, 167).
DragOverlay あり:
作業中の列の中の li 中心 (380, 128) opacity 0.3
列の外の DragOverlay 中心 (380, 167) opacity 1
DragOverlay なし:
作業中の列の中の li 中心 (380, 167) opacity 0.3
With DragOverlay, what follows the mouse is a separate, solidly drawn element. The original card in the column stays faded and sits at the position where it will land on release. Without DragOverlay, what follows the mouse is the faded original card itself. If you want to show where a card will land with a faded card, DragOverlay makes that easier to see.
You can also move cards between columns with the keyboard
KeyboardSensor is given sortableKeyboardCoordinates, so you can also move cards with the keyboard. I focused "Make an estimate" with the Tab key, then pressed Space, the → key, and Space in that order. The card moved into the Doing column.
後: 未着手: 画像を差し替える, 議事録をまとめる | 作業中: 見積もりを作る, ログイン画面を直す | 完了: (なし)
Next, I focused "Fix the login screen" with the Tab key and pressed Space and then the → key. It moved into the empty Done column. Pressing the → key again kept it in the Done column, the rightmost column. Finally, I pressed Space, and the card was dropped in the Done column.
When TypeScript reports errors in the dnd-kit type definitions
My test tsconfig.json had "skipLibCheck": true. When I removed it and ran tsc, the dnd-kit type definition files reported the following error in seven places.
node_modules/@dnd-kit/core/dist/components/DragOverlay/DragOverlay.d.ts(8,28): error TS2503: Cannot find namespace 'JSX'.
The dnd-kit type definitions use the global namespace JSX. However, in index.d.ts of @types/react 19.3.0, JSX is inside the React namespace and is not declared globally. My own .tsx files had no errors, and with skipLibCheck turned on, the type check passed.





