Last active
January 20, 2019 05:50
-
-
Save hozefaj/18965c1ec4b35eaa5ecb2c0dab7ca201 to your computer and use it in GitHub Desktop.
defer loading image
This file contains hidden or 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
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