Created
June 23, 2018 04:43
-
-
Save alfianlosari/7408aca6a0807ae5e1086e42af8da07e to your computer and use it in GitHub Desktop.
React Apollo GitHub Pagination Repos 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
import React, { Component } from "react"; | |
class Repos extends Component { | |
componentDidMount() { | |
window.addEventListener("scroll", this.handleOnScroll); | |
} | |
componentWillUnmount() { | |
window.removeEventListener("scroll", this.handleOnScroll); | |
} | |
handleOnScroll = () => { | |
// http://stackoverflow.com/questions/9439725/javascript-how-to-detect-if-browser-window-is-scrolled-to-bottom | |
var scrollTop = | |
(document.documentElement && document.documentElement.scrollTop) || | |
document.body.scrollTop; | |
var scrollHeight = | |
(document.documentElement && document.documentElement.scrollHeight) || | |
document.body.scrollHeight; | |
var clientHeight = | |
document.documentElement.clientHeight || window.innerHeight; | |
var scrolledToBottom = Math.ceil(scrollTop + clientHeight) >= scrollHeight; | |
if (scrolledToBottom) { | |
this.props.onLoadMore(); | |
} | |
}; | |
render() { | |
if (!this.props.entries && this.props.loading) return <p>Loading....</p>; | |
const repos = this.props.entries.edges || []; | |
return ( | |
<ul> | |
{repos.map(({ node }, idx) => ( | |
<li key={idx}> | |
<h3> | |
{node.name} - {node.owner.login} | |
</h3> | |
<p>{node.description}</p> | |
<p> | |
★ {node.stargazers.totalCount} -{" "} | |
{node.primaryLanguage && node.primaryLanguage.name}{" "} | |
</p> | |
</li> | |
))} | |
{this.props.loading && <h2>Loading...</h2>} | |
</ul> | |
); | |
} | |
} | |
export default Repos; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment