Is it possible to concatenate an img src in React?

柔情痞子 提交于 2020-07-05 12:25:30

问题


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}`} />


来源:https://stackoverflow.com/questions/48377234/is-it-possible-to-concatenate-an-img-src-in-react

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!