Created
September 16, 2016 04:03
-
-
Save blainegarrett/0b6d82d8f61b6dac34ebfb008e4a3cd4 to your computer and use it in GitHub Desktop.
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
// Simple React Class for managing the loading state of an image asset | |
var ImgLoader = React.createClass({ | |
loadHandler: function () { | |
this.setState({img_loaded: true}); | |
}, | |
getInitialState: function () { | |
return { | |
loading_src: 'https://dummyimage.com/700x700/ccc/ffffff.png&text=loading...', | |
img_loaded: false | |
} | |
}, | |
componentDidMount() { | |
var downloadingImage = new Image(); | |
downloadingImage.onload = this.loadHandler; | |
downloadingImage.src = this.props.src; | |
}, | |
render: function() { | |
var src = this.state.loading_src; | |
if (this.state.img_loaded) { | |
src = this.props.src | |
} | |
return (<img src={ src } className="img-responsive"/>); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment