Created
April 29, 2016 13:39
-
-
Save augustinr/1f61eb2cb7049c27a50caf1d1850f923 to your computer and use it in GitHub Desktop.
React ImgWithFallBack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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