Skip to content

How to update the Item State ? #300

Answered by clauderic
kkroeger93 asked this question in Q&A
Discussion options

You must be logged in to vote

This logic will always return -1:

const oldIndex = items.indexOf(active.id);
const newIndex = items.indexOf(over.id);

This is because your items array contains objects and not strings. Use findIndex instead:

const oldIndex = items.findIndex(item => item.id === active.id);
const newIndex = items.findIndex(item => item.id === over.id);

https://codesandbox.io/s/vigorous-mountain-rd1fm?file=/src/App.js

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@kkroeger93
Comment options

@Messa1
Comment options

@dev-xo
Comment options

@khawaja-Faizan
Comment options

Answer selected by kkroeger93
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants