I understand that avoiding the mutation of state is very important in react, although when it comes to avoiding the mutation of arrays in setState, I am wondering how necessary that is.
I was able to implement a way to change an entire array (length of 2) without mutating the original. However, it seems like a lot of unnecessary work and is not practical for large data sets. I am learning and experimenting and would like to know what the best practices are.
Can someone let me know if it is generally okay to completely update an array in state like the first example that I commented out, or if the longer way with the map method is a “better practice” ? Thank you
// setMapPosition([geoLocationPosition.lat, geoLocationPosition.lng])
// ? implemented callback so it returns an altered copy and doesnt
// ? mutate the original array
setMapPosition(prev => prev.map((item, index) => {
if(index === 0) {
item = geoLocationPosition.lat
}
else if(index === 1) {
item = geoLocationPosition.lng
}
return item
}))
console.log(mapPosition);