Skip to content

Instantly share code, notes, and snippets.

@cdaz5
Created February 19, 2019 13:07
Show Gist options
  • Save cdaz5/136a59e6dda9aca301bcc9ff60ffe269 to your computer and use it in GitHub Desktop.
Save cdaz5/136a59e6dda9aca301bcc9ff60ffe269 to your computer and use it in GitHub Desktop.
import React from 'react';
import debounce from 'lodash/debounce';
import { InfinitePanel } from '../shared';
const withInfiniteScroll = WrappedComponent => class InfiniteScroll extends WrappedComponent {
constructor(props) {
super(props);
this.infiniteRef = React.createRef();
this.debouncedHandleInfiniteScroll = debounce(this.handleInfiniteScroll, 150);
}
componentDidMount() {
document.addEventListener('scroll', this.debouncedHandleInfiniteScroll);
}
componentWillUnmount() {
document.removeEventListener('scroll', this.debouncedHandleInfiniteScroll);
}
handleInfiniteScroll() {
if (this.infinityRef.current) {
super.handleInfiniteScroll();
}
}
render() {
const { noPadding, noMargin, ...props } = this.props;
return (
<InfinitePanel
noPadding={noPadding}
noMargin={noMargin}
height="100%"
ref={this.infiniteRef}
flex
column
>
<WrappedComponent {...props} />
</InfinitePanel>
);
}
};
export { withInfiniteScroll };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment