Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Bryant Feld
seal-mask
.a{fill-rule:evenodd;}techdegree
Bryant Feld
Full Stack JavaScript Techdegree Student 26,144 Points

render image url in React

If I am getting an image url from an API call in react can I just pass the img url as a prop like this

 <div className="tributeimage">
     <img src = {props.tribute_img_url} alt="this is the guy/gal" />
     </div>

or will this not work (it does not appear be to working for me now)

1 Answer

Yes, provided it is a valid URL. Note that React doesn't care what the value is or anything, it just assumes this is a string, and knows src is a valid attribute for an img tag.

Also, because you mentioned you're getting it from an API, you may just need to be careful when you get it, because if the React component is rendered before you get the response from the API, you'll likely want to handle the state/props before that (e.g. have some sort of placeholder image while it waits for the response).