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
unshiftadding theaddedFoodin front of theeatenFoodListarray while the in second methodeatenFoodListis first destructured and thenaddedFoodis added.Need more context on what conditions you are updating the
eatenFoodListstate to find out how the first method is executing twice.