I'd like to update a state of one object in array, not updating the state of whole array.
const [users, setUsers] = useState;
const data = [
{
id: 1,
name: "John",
activeAccount: 1
},
{
id: 2,
name: "Mary",
activeAccount: 1
},
{
id: 3,
name: "Max",
activeAccount: 1
}
]
For example, John deactivate his account and state of activeAccount should be updated to 0.
So only one object should be updated (he has no data from other users and cannot update all array) - just example, why i use here user.id
const index = users.findIndex(obj => obj.id === user.id); // find index of object
users[index].activeAccount = 0
setUsers(users) // Nothing updated
I also tried to use Immutability Helper
import update from 'immutability-helper';
const index = users.findIndex(obj => obj.id === user.id); // find index of object
setUsers({
users: update(...users, {index: {activeAccount: {$set: 0 }}})
})
Could anyone help? Is there a solution?