Overview
This guide walks you through creating your first drag and drop interface with dnd-kit. You’ll learn the fundamental concepts by building a simple draggable item, then progress to creating a sortable list.This guide uses React examples. For other frameworks, the concepts are identical—only the syntax differs slightly.
Prerequisites
Before starting, make sure you have:1
Installed dnd-kit
Follow the installation guide to install the necessary packages for your framework.
2
Basic framework knowledge
Familiarity with React hooks (or equivalent in your framework) is helpful.
Your first draggable
Let’s start with the simplest possible example: a single draggable element.1
Set up the provider
Wrap your application (or the part that needs drag and drop) with The
DragDropProvider:App.tsx
DragDropProvider manages the drag and drop state and coordinates all draggable and droppable elements.2
Create a draggable component
Use the Key points:
useDraggable hook to make an element draggable:Draggable.tsx
- Each draggable needs a unique
id - Pass the element reference via the
elementprop - Use
isDraggingto provide visual feedback
3
Use your draggable
Import and render your draggable component:
App.tsx
You should now be able to drag the element around! The element will follow your cursor and return to its original position when released.
Adding a drop zone
Now let’s add a droppable area where you can drop the draggable element.1
Create a droppable component
Use the The
useDroppable hook:Droppable.tsx
isDropTarget property indicates when a draggable is hovering over this droppable.2
Handle drop events
Add event handlers to track when items are dropped:
App.tsx
Building a sortable list
Now let’s create something more practical: a sortable list where you can reorder items by dragging.1
Install the helpers package
The helpers package provides utilities for common operations like reordering:
2
Create a sortable item component
Use the Key differences from basic draggables:
useSortable hook for items in a sortable list:SortableItem.tsx
- Uses
useSortableinstead ofuseDraggable - Requires both
idandindexprops - Uses
directionBiasedcollision detection optimized for lists
3
Create the sortable list
Build the list with state management:The
SortableList.tsx
move helper automatically calculates the new array order based on the drag event.You now have a fully functional sortable list! Try dragging items to reorder them.
Adding visual feedback
Enhance the user experience with a drag overlay that follows the cursor:SortableList.tsx
DragOverlay renders a custom element that follows the cursor during drag operations.
Advanced features
Now that you have the basics down, explore these advanced features:Drag handles
Add a specific handle that must be grabbed to initiate dragging:
Modifiers
Restrict movement to an axis or snap to grid:
Multiple containers
Create complex layouts with multiple droppable containers and transfer items between them.
Custom sensors
Configure activation constraints and custom input methods:
Event lifecycle
Understand the drag and drop event lifecycle:1
beforedragstart
Fired before a drag operation begins. You can prevent the drag by calling
event.preventDefault().2
dragstart
Fired when a drag operation starts. Perfect for tracking analytics or updating UI state.
3
dragmove
Fired continuously as the draggable moves. Use sparingly for performance.
4
dragover
Fired when a draggable moves over a droppable. Ideal for visual feedback.
5
dragend
Fired when a drag operation completes, whether successful or cancelled. Update your data here.
Common patterns
Optimistic updates
Update the UI immediately during drag for a snappier feel:Conditional dropping
Control what can be dropped where:Disabled items
Temporarily disable dragging:Next steps
You’ve built your first drag and drop interfaces! Continue learning:API reference
Explore the complete API documentation
Examples
Browse more complex examples and patterns
Accessibility
Learn about keyboard navigation and screen reader support
Performance
Optimize your drag and drop for large lists