11

I am trying to build a very basic sortable using react dnd-kit, and everything works, except one thing : the first element of my sortable array doesn't move when it is dragged (it moves when other elements are dragged on it, which makes this even weirder).

Here is the CodeSandBox with the project : https://codesandbox.io/s/condescending-wright-scyqvo?file=/src/components/Item.js

Hope someone can help me, thanks a lot :)

2 Answers 2

37

Ok I solved it, but I'll leave it here just in case it can be useful to someone else.

The id given to the useSortable hook cannot be 0, and therefore the ids of the list should begin at 1.

4
  • 5
    You are a man of culture and honour.
    – Andre Pena
    Commented May 12, 2023 at 9:26
  • 1
    Didn't find anything about this, can you share some docs link?
    – SkyDancer
    Commented Jun 29, 2023 at 11:15
  • It's because 0 is falsy I think !
    – Trevodorax
    Commented Jun 30, 2023 at 12:46
  • I ran into the same issue. Thank you so much.
    – theeranitp
    Commented Apr 4 at 5:30
1

It's already solved but I don't see the solution I came up with. So when passing the ID to useSortable hook you can surround it with backticks like so:

const { attributes, listeners, setNodeRef, transform } = useDraggable({
  id: `${your_id}`,
});
1
  • 1
    Yea nice one, '0' is not falsy while 0 is :)
    – Trevodorax
    Commented Dec 19, 2023 at 14:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.