useSortable
The useSortable hook is an abstraction that composes the useDroppable and useDraggable hooks.
To function properly, the useSortable hook needs to be used within a descendant of a SortableContext provider higher up in the tree.

Usage

If you're already familiar with the useDraggable hook, the useSortable hook should look very familiar, since, it is an abstraction on top of it.
In addition to the attributes, listeners,transform and setNodeRef arguments, which you should already be familiar with if you've used the useDraggable hook before, you'll notice that the useSortable hook also provides a transition argument.
1
import React from 'react';
2
import {useSortable} from '@dnd-kit/sortable';
3
import {CSS} from '@dnd-kit/utilities';
4
5
function SortableItem(props) {
6
const {
7
attributes,
8
listeners,
9
setNodeRef,
10
transform,
11
transition,
12
} = useSortable({id: props.id});
13
14
const style = {
15
transform: CSS.Transform.toString(transform),
16
transition,
17
};
18
19
return (
20
<li ref={setNodeRef} style={style} {...attributes} {...listeners}>
21
{/* ... */}
22
</li>
23
);
24
}
Copied!

Properties

Listeners

The listeners property contains the activator event handlers for each Sensor that is defined on the parent DndContext provider.
It should be attached to the node(s) that you wish to use as the activator to begin a sort event. In most cases, that will be the same node as the one passed to setNodeRef, though not necessarily. For instance, when implementing a sortable element with a "drag handle", the ref should be attached to the parent node that should be sortable, but the listeners can be attached to the handle node instead.
For additional details on the listeners property, refer to the useDraggable documentation.

Attributes

The useSortable hook provides a set of sensible default attributes for draggable items. We recommend you attach these to your draggable elements, though nothing will break if you don't.
For additional details on the attributes property, refer to the useDraggable documentation.

Transform

The transform property represents the displacement and change of scale transformation that a sortable item needs to apply to transition to its new position without needing to update the DOM order.
The transform property for the useSortable hook behaves similarly to the transform property of the useDraggable hook for the active sortable item, when there is no DragOverlay being used.

Node ref

In order for the useSortable hook to function properly, it needs the setNodeRef property to be attached to the HTML element you intend on turning into a sortable element:
1
function SortableItem(props) {
2
const {setNodeRef} = useDraggable({
3
id: props.id,
4
});
5
6
return (
7
<li ref={setNodeRef}>
8
{/* ... */}
9
</li>
10
);
11
}
Copied!
Keep in mind that the ref should be assigned to the outer container that you want to become draggable, but this doesn't necessarily need to coincide with the container that the listeners are attached to:
1
function SortableItem(props) {
2
const {arguments, listeners, setNodeRef} = useDraggable({
3
id: props.id,
4
});
5
6
return (
7
<li ref={setNodeRef}>
8
{/* ... */}
9
<button {...listeners} {...arguments}>Drag handle</button>
10
</li>
11
);
12
}
Copied!
Since the useSortable hook is simply an abstraction on top of the useDraggable and useDroppable hooks, in some advanced use cases, you may also use the setDroppableNodeRef and setDraggableNodeRef properties to connect them to different nodes. For example, if you want the draggable element to have a different dimension than the droppable element that will be sortable:
1
function SortableItem(props) {
2
const {setDraggableNodeRef, setDroppableNodeRef} = useDraggable({
3
id: props.id,
4
});
5
6
return (
7
<li ref={setDroppableNodeRef}>
8
{/* ... */}
9
<button ref={setDraggableNodeRef}>Drag me</button>
10
</li>
11
);
12
}
Copied!

Transition

Refer to the transition argument documentation below.

Arguments

Identifier

The id argument is a string that should be a unique identifier.
Since the useSortable is an abstraction on top of the useDroppable and useDraggable hooks, which both require a unique identifier, the useSortable hook also requires a unique identifier.
The argument passed to the id argument of useSortable should match the id passed in the items array of the SortableContext provider.

Disabled

If you'd like to temporarily disable a sortable item from being interactive, set the disabled argument to true.

Transition

The transition argument controls the value of the transition property for you. It conveniently disables transform transitions while not dragging, but ensures that items transition back to their final positions when the drag operation is ended or cancelled.
It also disables transitions for the active sortable element that is being dragged, unless there is a DragOverlay being used.
The default transition is 250 milliseconds, with an easing function set to ease, but you can customize this and pass any valid CSS transition timing function.
1
const {
2
transition,
3
} = useSortable({
4
transition: {
5
duration: 150, // milliseconds
6
easing: 'cubic-bezier(0.25, 1, 0.5, 1)',
7
},
8
});
Copied!
Make sure you pass the transition style property to the same node that has the transform property applied:
1
import React from 'react';
2
import {useSortable} from '@dnd-kit/sortable';
3
import {CSS} from '@dnd-kit/utilities';
4
5
function SortableItem(props) {
6
const {
7
transform,
8
transition,
9
} = useSortable({id: props.id});
10
11
const style = {
12
transform: CSS.Transform.toString(transform),
13
transition,
14
};
15
16
return (
17
<li style={style}>
18
{/* ... */}
19
</li>
20
);
21
}
Copied!
If you prefer, you may also use CSS variables to manage the transform and transition properties:
1
import React from 'react';
2
import {useSortable} from '@dnd-kit/sortable';
3
import {CSS} from '@dnd-kit/utilities';
4
5
function SortableItem(props) {
6
const {
7
transform,
8
transition,
9
} = useSortable({id: props.id});
10
11
const style = {
12
'--translate-x': transform ? transform.x : 0,
13
'--translate-y': transform ? transform.y : 0,
14
'--transition': transition,
15
};
16
17
return (
18
<li style={style}>
19
{/* ... */}
20
</li>
21
);
22
}
Copied!
To disable transitions entirely, set the transition argument to null:
1
const {
2
transition,
3
} = useSortable({
4
transition: null,
5
});
Copied!
If you prefer to manage transitions yourself, you may also choose to do so, but this isn't something we recommend.

Sorting strategy

Optionally, you can pass a local sorting strategy that differs from the global sorting strategy passed to the parent SortableContext provider.
Last modified 9mo ago