nvancouver
nvancouver

Reputation: 147

React - Re-arrange/swap elements in an array map

I'm creating a simple list that I'd like to be able to "sort / rearrange" via up/down arrows, like so : https://codesandbox.io/s/stupefied-kilby-thcd6?file=/src/App.js

I am currently using array.splice to return my "from" item, and then once again using splice to insert the "from" item at its desired location

I can currently see this occurring "properly" in the console.logs, however I am unable to re-render my map to reflect the index positions, I have provided my map with a proper key (the ID's), and so I believe I've covered my base there.

When calling setState with my "sorted" array, I can see no visual change, and I'm looking for suggestions, thanks!

Upvotes: 0

Views: 394

Answers (1)

usafder
usafder

Reputation: 816

Just use the spread operator when setting options as shown below:

setOptions([...newOptions]);

Upvotes: 1

Related Questions