apps/docs/react/hooks/use-sortable.mdx
import {Story} from '/snippets/story.mdx'; import {CodeSandbox} from '/snippets/sandbox.mdx'; import {sortableStyles} from '/snippets/code.mdx';
<Story id="react-sortable--example" height="320" hero />The useSortable hook requires an id and an index. It accepts all the same options as the Sortable class. Refer to the Input section below for more information.
export const code = ` import {useSortable} from '@dnd-kit/react/sortable';
function Sortable({id, index}) { const {ref} = useSortable({id, index});
return ( <li ref={ref} className="item">Item {id}</li> ); }
export default function App() { const items = [1, 2, 3, 4];
return ( <ul className="list"> {items.map((id, index) => <Sortable key={id} id={id} index={index} /> )} </ul> ); } `.trim();
<CodeSandbox files={{ 'App.js': {code, active: true}, 'styles.css': {code: sortableStyles, hidden: true}, }} height={455} previewHeight={180} />
The useSortable hook accepts all of the same arguments as the useDraggable hook and useDroppable hooks, as well as additional arguments that are specific to sortable elements.
<ParamField path="easing" type="string">
The easing function to use for the transition.
</ParamField>
<ParamField path="idle" type="boolean">
Whether the sortable item should transition to its new position when its index changes, but there is no drag operation in progress.
</ParamField>
The useSortable hook returns an object containing the following properties: