Fixing Array State Updates in React with toSpliced()

When using splice() in #ReactJS to update an array state variable, I ran into unexpected behavior. Instead, using a newer #JavaScript method called toSpliced() and updating the array with the useState, was able to fix this. I could have just copied the array, spliced the copy, and then set the state value of the array to equal the spliced copy, but toSpliced() seemed simpler. The toSpliced() method is supported in all major browsers as of 2023. Before trying splice() and toSpliced(), I had a filter function, as can be seen in the included code snippet from the relevant commit.

  • graphical user interface, text, application, email

To view or add a comment, sign in

Explore content categories