Skip to content

Instantly share code, notes, and snippets.

@Shobhit1
Created June 2, 2016 14:58
Show Gist options
  • Save Shobhit1/e9801c5442b0255117eb7458cd0941a2 to your computer and use it in GitHub Desktop.
Save Shobhit1/e9801c5442b0255117eb7458cd0941a2 to your computer and use it in GitHub Desktop.
export default class InfiniteLoader extends Component {
  componentDidMount() {
    window.addEventListener('scroll', this.handleScroll.bind(this))
  }

  componentWillUnMount() {
    window.removeEventListener('scroll', this.handleScroll.bind(this))
  }

  handleScroll(e) {
    e.preventDefault()
    const { showLoader, loadNextSet } = this.props
    // this function will be triggered if user scrolls
    setTimeout(() => {
      const windowHeight = document.documentElement.clientHeight
      const inHeight = window.innerHeight
      const scrollT = (window.pageYOffset !== undefined)
        ? window.pageYOffset
        : (document.documentElement || document.body.parentNode || document.body).scrollTop
      const totalScrolled = scrollT + inHeight
      if (totalScrolled + 100 > windowHeight) { // user reached at bottom
        if (!showLoader) { // to avoid multiple request
          // dispatch(SearchActions.searchNextPage())
          window.console.log('loadnextset')
          loadNextSet()
        }
      }
    }, 2500)
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment