You can receive data using the onLoad attribute.
class ImgLoader extends Component {
constructor(props) {
super(props);
this.state = {
error: false,
};
}
onImgLoad = ({ target: img }) => {
this.setState({
width: img.width,
height: img.height,
});
};
onImgError = e => {
e.target.src = '/path/no-picture.jpg';
this.setState({
error: true,
});
};
render() {
const { image, alt } = this.props;
const { width, height, error } = this.state;
console.log(width, height, error);
return <img onLoad={this.onImgLoad} src={image} alt={alt} onError={this.onImgError} />;
}
}
source
share