Answer the question
In order to leave comments, you need to log in
How to change background-image only after loading the react image itself?
the element code is:
<div
className={'photo-item'}
style={{backgroundImage: 'url(' + this.props.item.preview + ')'}}
>
</div>
Answer the question
In order to leave comments, you need to log in
This can be done by using new Image()
and subscribing to the onload
event.
Example taken from redux tutorial code
class BigPhoto extends React.Component {
state = {
isLoading: false,
}
componentDidMount() {
this.loadImage(this.props.url)
}
loadImage = src => {
this.setState({ isLoading: true })
let img = new Image()
img.onload = () => {
this.setState({ isLoading: false })
}
img.src = src
}
render() {
const { isLoading } = this.state
const { url } = this.props
return isLoading ? <p>Загружаю...</p> : <img src={url} alt="big vk" />
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question