Is it possible to concatenate an img src in React?

2020-07-20 15:55发布

问题:

This might be a silly question but I'm trying to concatenate the source of an image in React to be rendered on the screen, but it's not working.

<img src=`https://www.cryptocompare.com/{this.state.cryImage}` />

In this.state.cryImage, I only get the second half of the link (I'm pulling data from an API) so for example it would be something like:

media/19633/btc.png

Am I concatenating incorrectly or is this just not possible?

回答1:

You've missed $ sign and brackets for attribute

<img src={`https://www.cryptocompare.com/${this.state.cryImage}`} />


回答2:

You forgot to add {} in src prop:

<img src={`https://www.cryptocompare.com/${this.state.cryImage}`} />