legalizeSINCE88
legalizeSINCE88

Reputation: 97

Source of Img should be dynamic

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

Answers (2)

Anurag Srivastava
Anurag Srivastava

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

Will Jenkins
Will Jenkins

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

Related Questions