I have a state object that contains an object and I am trying to add/remove items from it without success:
const [currentUser,setCurrentUser]=useState(Object)
and the Object looks like:
{username:"username",
array:[0,...,N]}
This tells me that it does not recognize the array...
setCurrentUser([...currentUser.array,index])
CodePudding user response:
You said your state contains an array, but your code shows it contains an Object instead. So to update only the array inside the object, you should do:
setCurrentUser({...currentUser, array: [...currentUser.array, index]})