【发布时间】:2019-11-23 20:25:05
【问题描述】:
我在 ReactJS 中工作,并且有一个存储在 State 中的照片的 URL。我想使用该 photoURL 在我的组件渲染中调用图像。但是当我使用普通的src={this.state.photoURL} 时,它会引发错误。
理想情况下,我可以将它用作容器中的背景图像,如下所示:
<div className='photo-cont' style='background-image:url({this.state.photoURL}); background-size:cover;'
我已经尝试过这个,并且作为一个普通的 img,在花括号周围有和没有引号,有和没有花括号本身。
class Foo extends Component {
state: {
photoURL: 'www.foobar.com/foo.jpg',
}
render () {
return(
<img src={this.state.photoURL}></img>
)
}
}
我不断收到以下错误:'TypeError: Cannot read property 'photoURL' of null'
【问题讨论】:
-
把
state: {...改成state = {...能用吗?
标签: reactjs attributes state jsx