how to be sure if image is loaded in react? code example
Example: check if image is still loading reactjs
class App extends React.Component {
constructor(){
super();
this.state = {loaded: false};
}
render(){
return (
<div>
{this.state.loaded ? null :
<div
style={{
background: 'red',
height: '400px',
width: '400px',
}}
/>
}
<img
style={this.state.loaded ? {} : {display: 'none'}}
src={this.props.src}
onLoad={() => this.setState({loaded: true})}
/>
</div>
);
}
}
ReactDOM.render(<App src={`http://placehold.it/400x400`} />, document.getElementById('root'));