Skip to content

Instantly share code, notes, and snippets.

@zzarcon
Created September 10, 2017 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zzarcon/99a8feb75015c503441f41330c60f4d4 to your computer and use it in GitHub Desktop.
Save zzarcon/99a8feb75015c503441f41330c60f4d4 to your computer and use it in GitHub Desktop.
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