Skip to content

Instantly share code, notes, and snippets.

@hozefaj
Last active January 20, 2019 05:50
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 hozefaj/18965c1ec4b35eaa5ecb2c0dab7ca201 to your computer and use it in GitHub Desktop.
Save hozefaj/18965c1ec4b35eaa5ecb2c0dab7ca201 to your computer and use it in GitHub Desktop.
defer loading image
export default class Hero extends React.Component {
state = {
showImages: false,
};
componentDidMount() {
// load images when images comes within the viewport once user starts scrolling
const domRect = this.sectionElement && this.sectionElement.getBoundingClientRect();
if (domRect && window.innerHeight - domRect.top >= 0) {
this.enableImages();
return;
}
}
enableImages() {
this.setState({
showImages: true,
});
}
renderImage(image) {
if (this.state.showImages && image) {
return <img src={image.src} className={`${ORGANISM_NAME}__image`} alt={image.alt} />;
}
return null;
}
render() {
....
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment