samedi 21 août 2021

Antd Badge count not updating in react for the last value

screenshot of ui and react component props

As you can see in the image, cart.totalQuantity is 0, but the Antd badge still shows one. Actually it is working for n...2, no issues there. only when we delete the last item in the cart it is not updating. Any idea ?

Code for badge:

const { cart } = useContext(AppContext);
<Badge count={cart.totalQuantity} offset={[-2, 5]}>
   <ShoppingCartOutlined
      className="mx-2"
      style=
    />
</Badge>

Code for AppProvider:

export const AppProvider = (props) => {
  const [cart, updateCart] = useState({
    items: [],
    totalAmount: 0,
    totalQuantity: 0,
  });

  useEffect(() => {
    const cookieCart = Cookie.get("cart");

    if (cookieCart !== undefined && cookieCart.length > 0) {
      let totalCount = 0;
      let totalPrice = 0;
      JSON.parse(cookieCart).forEach((item) => {
        totalCount += item.quantity;
        totalPrice += item.price * item.quantity;
        updateCart({
          items: JSON.parse(cookieCart),
          totalAmount: totalPrice,
          totalQuantity: totalCount,
        });
      });
    } else {
      updateCart({
        items: [],
        totalAmount: 0,
        totalQuantity: 0,
      });
    }
  }, []);

}




Aucun commentaire:

Enregistrer un commentaire