How to update the Item State ? #300
Answered
by
clauderic
kkroeger93
asked this question in
Q&A
-
Hey there, I'm currently trying to use the Sortable hook. So far I did achieved to make everything draggable the way I like, unfortunately somehow its not possible for me to update the state which provides all the items. Attached you can find also an example of my code. I'm glad for every suggestion to solve my Problem :)
|
Beta Was this translation helpful? Give feedback.
Answered by
clauderic
May 28, 2021
Replies: 1 comment 4 replies
-
This logic will always return const oldIndex = items.indexOf(active.id);
const newIndex = items.indexOf(over.id); This is because your items array contains objects and not strings. Use 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 |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
kkroeger93
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This logic will always return
-1
:This is because your items array contains objects and not strings. Use
findIndex
instead:https://codesandbox.io/s/vigorous-mountain-rd1fm?file=/src/App.js