Closed stevesizer closed 3 years ago
The <SortableContext>
component requires that you pass it the sorted array of the unique identifiers associated to each sortable item via the items
prop. This array should look like ["1", "2", "3"]
, not [{id: "1"}, {id: "2}, {id: "3}]
.
All you have to do is map your items array to an array of strings that represent the unique identifiers for each item:
export default function Column() {
const [items, setItems] = useState([
{
id: "1",
name: "Clean Car",
description: "Take the car to get cleaned",
},
{
id: "2",
name: "Pick up kids",
description: "Pick up kids and take them to soccer pratice",
},
{
id: "3",
name: "Walk Dog",
description: "Meet up with John and take dog for a walk",
},
]);
const itemIds = useMemo(() => items.map((item) => item.id), [items]); // ["1", "2", "3"]
/* ... */
<SortableContext items={itemIds}>
Hi @clauderic
Does ids like ['6d0900e9', 'de75dce5']
work for this?
Hi @clauderic
Does ids like
['6d0900e9', 'de75dce5']
work for this?
yes
I have id's like this and it does not work ['1686', '3337', '3225', '3313']
https://codesandbox.io/s/lucid-yalow-77hh97?file=/src/DndWithObjects.tsx
To save others time who may have missed it in stevesizers code, you also need to replace the indexOf method in handleDragEnd with the findIndex method. This is because indexOf is searching the array for a primitive and will not find it if the elements are objects, whereas you can be more specific about what you're looking for using findIndex's callback
I just came across this same case and found this issue after a few hours of searching. Shouldn't this case be in the documentation (especially the useMemo(...)
, and the handleDragEnd(event)
that need to be modified) ?
edit:nvm not workin
Hi,
I have modified your basic example to try and use it with an object array, but doing so stops the animations from happening? Is there any reason for this?
Column.js
Item.js