Home > Blockchain >  I want to change the image I am using whenever the product is added to cart
I want to change the image I am using whenever the product is added to cart

Time:02-15

I am using this right now, need to add a condition in such a way that whenever the getCartCount is 0 it should show image as bag_empty instead of bag and when the cart count is more than 0 it should show bag

<Badge count={getCartCount()}>
              <img
                className="bag__img"
                src={bag}
                alt=""
                onClick={user ? handleOpenBag : handleDynamicLink}
                onm ouseOver={() => setbag(hoverbag)}
                onm ouseOut={() => setbag(cart)}
              />
</Badge>

CodePudding user response:

You could use a ternary operator like below, assuming that you have your empty bag image in your public folder.

<Badge count={getCartCount()}>
    <img
      className="bag__img"
      src={getCartCount()>0 ? bag : "/emty-bag-goes-here.png"}
      alt=""
      onClick={user ? handleOpenBag : handleDynamicLink}
      onm ouseOver={() => setbag(hoverbag)}
      onm ouseOut={() => setbag(cart)}
    />
</Badge>
  • Related