简体   繁体   English

内联动态更改背景图片 React

[英]Inline dynamically change background image React

I'm trying to learn React using movieDB.我正在尝试使用 movieDB 学习 React。 Each time a user searches a movie the background image should update.每次用户搜索电影时,背景图像都应更新。 However, it states I have an unexpected token.但是,它指出我有一个意外的令牌。 What am I missing here?我在这里缺少什么?

class Movie extends Component {

var styles = {
    var imgURL = 'https://image.tmdb.org/t/p/' + this.props.movie.poster_path;
    backgroundImage: 'url(' + imgUrl + ')',
    backgroundPosition: "center",
    backgroundSize: "cover"
}

render() {
    return (
        <div className="Movie" style={styles}>
            <div>
                <img src={'https://image.tmdb.org/t/p/w500/' + this.props.movie.poster_path} />
            </div>
            <strong>{this.props.movie.tagline}</strong>
            <h3>{this.props.movie.title}</h3>
            <p>{this.props.movie.overview}</p>
        </div>
    );
}

} }

var imgUrl inside the object definition...对象定义中的var imgUrl ...

And yeah the whole block should go inside the render method是的,整个块都应该放在渲染方法中

I figured it out.我想到了。 A community to help not criticize...一个帮助不批评的社区......

    var styles = {
        backgroundImage: `url(https://image.tmdb.org/t/p/w500/${this.props.movie.poster_path})`,
        backgroundPosition: "center",
        backgroundSize: "cover"
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM