How to use sortable list
SortableList
component is a powerful utility that enables drag-and-drop sorting functionality within your React application. Built on top of @dnd-kit/core
, it allows for intuitive list reordering with touch and mouse support. Here’s how you can incorporate it into your project.
SortableList
component with your data and a function to render each item. Here’s an example:
SortableList
allows you to handle events when sorting starts and ends. You can use these to manage state or perform actions based on the new order:
SortableList
uses sensors to handle drag events. You can customize their behavior, such as the delay and tolerance for touch sensors or the distance for mouse sensors, to tailor the user experience to your application’s needs.
In the component code, these are set through the useSensor
hook. Adjust these settings as needed to achieve the desired responsiveness.
SortableItem
component. This component is responsible for making each item draggable. Ensure that the id
prop matches the unique identifier in your data: