Last active
October 5, 2016 00:12
-
-
Save lucasbento/6b2f2e02a691fd87ae2340f84214837c to your computer and use it in GitHub Desktop.
Easy infinity loading with React
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
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