【发布时间】:2018-03-19 21:58:50
【问题描述】:
我正在尝试设置一个名为 FullscreenImage 的反应组件,它将我的图像显示为 100% 的视差样式的宽度/高度。由于我将更频繁地使用这个组件,我认为通过 props 传递图片的 URL 是一个好主意。问题是,如果我将 URL 作为道具传递,它不会加载图像,而只是插入 URL。我怎样才能加载图片?我在我的网页上看到,静态插入的 URL 会转换为前缀为 static/media/ 的 URL。
FullscreenImage.jsx
let defaultStyle = {
minHeight: '100vh !important',
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
backgroundAttachment: 'fixed',
backgroundPosition: 'center',
backgroundRepeat: 'no-repeat',
backgroundSize: 'cover',
overflow: 'hidden'
};
class FullscreenImage extends Component {
constructor() {
super();
this.state = {
defaultStyle: defaultStyle,
background: {}
};
}
componentDidMount() {
this.setState({background: this.props.source.uri});
}
render() {
console.log(this.state);
return (<div style={{
...defaultStyle,
backgroundImage: this.state.background
}}></div>);
}
}
export default FullscreenImage;
App.jsx
let picture1 = {
uri: "url(../img/austin-neill-247237-unsplash.jpg)"
};
class App extends Component {
render() {
return (<section>
<FullscreenImage source={picture1}>
</section>);
}
}
export default Login;
提前致谢。
【问题讨论】:
标签: javascript image reactjs object