FrontendDeveloper.in

React Track Detail

What are the preferred and non-preferred array operations for updating the state?

The below table represent preferred and non-preferred array operations for updating the component state.

ActionPreferredNon-preferred
Addingconcat, [...arr]push, unshift
Removingfilter, slicepop, shift, splice
Replacingmapsplice, arr[i] = someValue
sortingcopying to new arrayreverse, sort

If you use Immer library then you can able to use all array methods without any problem.