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

Adding more than one conditional in an <img> src?

This is a line of code in my react app

<img className='ArrowIcon' src={data?.quote_data[0].change < 0 && arrowdown}></img>

I am trying to change the icon when the data is less than 0 and greater than 0 but dont know how to include both in the src of the img.

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

Is there any way to add in that when data?.quote_data[0].change > 0 it returns arrowup. When I use a comma it gives and error.

Thanks 🙂

>Solution :

As in the title of your question – you need a conditional expression, so use the conditional operator.

<img
  className='ArrowIcon'
  src={
    data?.quote_data[0].change < 0 ? arrowdown
    : data?.quote_data[0].change > 0 ? arrowup
    : defaultvalue
  }
></img>

(where defaultvalue would be if neither condition is fulfilled)

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