Skip to main content

Overview

The basic sortable list is the fundamental building block for implementing drag-and-drop sorting in dnd-kit. This example demonstrates how to create a vertical list where items can be reordered by dragging.

Quick Start

Here’s the simplest possible sortable list implementation:

Full Implementation

For a production-ready sortable list with drag handles and visual feedback:

Key Concepts

The useSortable Hook

The useSortable hook is the core of sortable functionality. It accepts the following key props:
  • id: A unique identifier for the sortable item
  • index: The current position of the item in the list
  • element: A ref to the DOM element (optional, can use the returned ref instead)
  • handle: A ref to the drag handle element (optional, if not provided, the entire element is draggable)
The hook returns:
  • ref: Callback ref to attach to your sortable element
  • handleRef: Callback ref to attach to your drag handle
  • isDragging: Boolean indicating if this item is currently being dragged
  • isDropTarget: Boolean indicating if this item is a valid drop target

State Management with move

The move helper function from @dnd-kit/helpers automatically handles the reordering logic:
This function:
  • Extracts the source and target indices from the drag event
  • Reorders the array accordingly
  • Handles edge cases automatically

Element References

You can attach the sortable behavior to your elements in two ways: Using the returned ref (simpler):
Using state (more control):

Drag Handles

Drag handles allow users to drag items only by grabbing a specific part of the item:
Without a handle, the entire element is draggable. This is useful for simple lists but can interfere with other interactions like text selection or button clicks.

Styling During Drag

Use the isDragging boolean to style items during drag operations:

Performance Tips

  1. Use stable keys: Always use unique, stable IDs as keys, not array indices
  2. Memoize items: Use React.memo for individual sortable items to prevent unnecessary re-renders
  3. Optimize large lists: For lists with hundreds of items, consider virtualization (see the virtualized lists example)

Next Steps