Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class ImgDimensions extends React.Component {
state = {width: 0, height: 0, errored: false};
constructor() {
this.onLoad = this.onLoad.bind(this);
this.onError = this.onError.bind(this);
}
onLoad({target: {height, width}}) {
this.setState({width, height});
}
onError() {
this.setState({errored: true});
}
render() {
const {width, height} = this.state;
return (
<div>
<div>{width}x{height}</div>
<img onLoad={this.onLoad} onError={this.onError} />
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment