Created
February 21, 2020 12:45
-
-
Save SergioGeeK7/c146102c7c0c8d5c7a83aa92fb8d0e1a to your computer and use it in GitHub Desktop.
react lazy load image component
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
// Image.js | |
import React from "react"; | |
import LazyLoad from "react-lazyload"; | |
export default class Image extends React.PureComponent { | |
state = { | |
loaded: false | |
}; | |
onLoad = () => { | |
this.setState({loaded: true}); | |
}; | |
render() { | |
const {loaded} = this.state; | |
const {className, ...props} = this.props; | |
const loadingClassName = loaded ? "" : "image-loading"; | |
const placeholder = ( | |
<div className={`${loadingClassName} ${className}`} /> | |
); | |
return ( | |
<LazyLoad once placeholder={placeholder}> | |
<img | |
{...props} | |
onLoad={this.onLoad} | |
className={`${loadingClassName} ${className}`} | |
/> | |
</LazyLoad> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment