Skip to content

Instantly share code, notes, and snippets.

@SergioGeeK7
Created February 21, 2020 12:45
Show Gist options
  • Save SergioGeeK7/c146102c7c0c8d5c7a83aa92fb8d0e1a to your computer and use it in GitHub Desktop.
Save SergioGeeK7/c146102c7c0c8d5c7a83aa92fb8d0e1a to your computer and use it in GitHub Desktop.
react lazy load image component
// 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