Reputation: 97
how can i load an img src dynamic with React ? I tried the following:
const country = this.state.countryCode;
<img src="https://www.countryflags.io/"+country+"/shiny/64.png"/>
But i got the error:
Parsing error: Unexpected token
Upvotes: 0
Views: 37
Reputation: 14433
You have forgotten curly braces. Also, you can create the url string before and use it:
const url = "https://www.countryflags.io/"+this.state.countryCode+"/shiny/64.png";
<img src={url}/>
Upvotes: 0
Reputation: 9887
This isn't React-specific, it's because you have the +
in the src.
Use a string template literal with back-ticks:
src=`https://www.countryflags.io/${country}/shiny/64.png`
Upvotes: 2