When I tried to set the state of my list, I tried two methods.
setEatenFoodList(prevList => {
prevList.unshift(addedFood);
return prevList;
})
This method adds the addedFood two times.
setEatenFoodList([...eatenFoodList, addedFood])
When this method adds it one time, which is what I want. But, I want to know the difference between these two methods.
As to your last question what both methods are doing are similar with
unshift
adding theaddedFood
in front of theeatenFoodList
array while the in second methodeatenFoodList
is first destructured and thenaddedFood
is added.Need more context on what conditions you are updating the
eatenFoodList
state to find out how the first method is executing twice.