I'm a bit of a rookie to react I was wondering why this happens: I have a toggle activate button which toggles the selected item activation
const handleToggleActivation = async () => {
...
setData((prev) => {
const newState = [...prev]
newState[selectedRow.index].isActive = !prev[selectedRow.index].isActive;
return newState;
})
...
Somewhy this piece of code won't work unless I make the change in the code below (which is saving the current state of isActive outside the setData) otherwise the new state will be exactly the same as the previous state (isActive value doesn't toggle)
const handleToggleActivation = async () => {
...
let isActive = data[selectedRow.index].isActive
setData((prev) => {
const newState = [...prev]
newState[selectedRow.index].isActive = !isActive;
return newState;
})
...
Any ideas why this happens?