Skip to content

Instantly share code, notes, and snippets.

@augustinr
Created April 29, 2016 13:39
Show Gist options
  • Save augustinr/1f61eb2cb7049c27a50caf1d1850f923 to your computer and use it in GitHub Desktop.
Save augustinr/1f61eb2cb7049c27a50caf1d1850f923 to your computer and use it in GitHub Desktop.
React ImgWithFallBack
class ImgWithFallBack extends Component {
constructor() {
super();
this.state = { displayCallBack: false };
}
onError() {
this.setState({ displayCallBack: true });
}
componentWillReceiveProps() {
this.setState({ displayCallBack: false });
}
render() {
const { src, FallbackComponent } = this.props;
const { displayCallBack } = this.state;
return (
<div>
{ (displayCallBack || !src) ?
<FallbackComponent /> : <img src={src} onError={this.onError} />
}
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment