Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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}
                onMouseOver={() => setbag(hoverbag)}
                onMouseOut={() => setbag(cart)}
              />
</Badge>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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}
      onMouseOver={() => setbag(hoverbag)}
      onMouseOut={() => setbag(cart)}
    />
</Badge>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading