Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
display an image in react based on a response

I want to display an icon only if the answer is yes?

{
 <div className={"options"}>
  <img src={iconteleconsult} alt={"teleconsultation"}/>
 </div>
}  
            
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

here is the code sample this will help:

response
    ? <div className={"options"}>
        <img src={iconteleconsult} alt={"teleconsultation"} />
      </div>
    : null
about 4 years ago · Juan Pablo Isaza Report

0

You can do like this way also render image conditionally.

condition && <img ... />

Example:

const { forwardRef, useState } = React;

const Parent = () => {
  const [isShowImage, setShowImage] = useState(4);

  const imageShowHandler = () => {
    setShowImage(isShowImage => !isShowImage);
  };

  return (
    <div>
      { isShowImage && <div className={"options"}>
         <img src={'https://image.shutterstock.com/image-photo/silhouette-boy-playing-cricket-evening-600w-1605103354.jpg'} alt={"teleconsultation"} />
      </div>}
      <br />
       <button onClick={imageShowHandler}>Toggle Image</button>
    </div>
  );
};


ReactDOM.render(<Parent />, document.getElementById("root"));
<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
<div id="root"></div>

about 4 years ago · Juan Pablo Isaza Report

0

Minimal code

Define a default image in a state and update it based on some condition.

const [defaultImage, setDefaultImage] = useState<image> // your image 

useEffect(() =>{
    if(someCondition) {
        setDefaultImage('newImage');
    }
}, [])


{
    <div className={"options"}>
        <img src={defaultImage} alt={"teleconsultation"}/>
    </div>
}      

This way in future if you intend to display images mulitple times no if/else would be needed at all places. Same code would work too.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!