const filteredata= basketdata.filter(obj => {
return obj.id !== item.id;
});
setBasket([...filteredata]);
console.log('basketdata',basketdata);
I am removing items from my array when same items available. After removing item trying to print the array in console but it doesn’t printing the actual value. Also I know that setState()
will not update immediately.
How can I solve this issue?
2
Answers
Use a
useEffect
-hook to listen for changes to the variableThe main issue was filter function does not change the original array it returns a new array thats why when you were printing the basketdata it was showing all the item.
Solution:
print the filteredata in the console log.