I have been working on a next.js project, and I have a part that acts as a shopping cart. When you click on an item, it adds the item image to a list and renders all the selected items. To remove the item, you can click on the small icon. When the small icon is clicked, I splice that list and update the prop, but it doesn't update until adding another item. I have seen this question, but when I implement it, it gives an error. I have also read this article, but it also doesn't like that. I have also posted my project on code on code sandbox here.
var cartImages = [];
export default function Home({ data }) {
const removeitem = (index) => {
cartImages.splice(index, 1);
setCartImg(cartImages);
};
const [cartImg, setCartImg] = useState(cartImages);
return (
<Popup onClick={removeitem} />);
}
Thanks for you're time!