Skip to content

Instantly share code, notes, and snippets.

@lucasbento
Last active October 5, 2016 00:12
Show Gist options
  • Save lucasbento/6b2f2e02a691fd87ae2340f84214837c to your computer and use it in GitHub Desktop.
Save lucasbento/6b2f2e02a691fd87ae2340f84214837c to your computer and use it in GitHub Desktop.
Easy infinity loading with React
class SomeInfinityLoadingSearch extends Component {
state = {
isLoading: false,
}
handleScroll = () => {
if (((window.innerHeight + window.scrollY) >= document.body.offsetHeight) && !this.state.isLoading) {
this.setState({
isLoading: true,
});
// Load more content
// Only call after loading finishes
this.setState({
isLoading: false,
});
}
}
componentDidMount = () => window.addEventListener('scroll', this.handleScroll)
componentWillUnmount = () => window.removeEventListener('scroll', this.handleScroll)
render() {
const { content } = this.props;
return (
<div>
<div>
{content ?
content.map(item => (
<SomeRandomItem
key={item.id}
item={item}
/>
)) : null
}
</div>
{this.state.isLoading ?
<div style={styles.loading}>
Loading...
</div>
: null
}
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment