Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React/Redux - Infinite Scrolling/Load More from API

In Short: I am fetching 21 JSON Objects from an API with X amount of Objects, I then want to fetch the next 21 Objects from the API when the user scrolls down the page. I have tried several options without luck, and my last option is to ask the clever developers in here.

I have never tried this before, so if someone would be kind and showcase a tip/solution, making a example out of my component, that would be much appreciated!

I'v tried using https://github.com/RealScout/redux-infinite-scroll, but I cant wrap my head around it.

Here is my component, where I am fetching all the teams on componentDidMount and then getting all the JSON objects from my reducer:

    import React, { Component } from 'react'
    import { APIManager, DateUtils, YouTubeID } from '../../utils'
    import actions from '../../actions'
    import { connect } from 'react-redux'
    import  { Link, browserHistory } from 'react-router'


    class teams extends Component {
      constructor () {
        super()
        this.state = {

        }
      }

      selectTeam(team, event){
      event.preventDefault()
      this.props.selectTeam(team)
     }

      componentDidMount(){
        if(this.props.teams != null){
          return
        }
        this.props.fetchTeams()
      }

      componentDidUpdate(){
        if(this.props.teams != null){
          return
        }
        this.props.fetchTeams()
      }

      render(){
        return(
 <div className="scrollable-content-container">
 <ol className="portal-list-members debutants scrollable">

{(this.props.teams == null) ? null :
  this.props.teams.map((team, i) => {

    return (
      <li onClick={this.selectTeam.bind(this, team.teamname)} key={i} className="group">
        <h3>
        <Link to={'/team'} style={{color: '#444', textTransform: 'capitalize'}} className="url hoverable" href="#">
          <img style={{height: '160px', width: '160px'}} className="photo" src={"images/"+team.image}/>
        {team.teamname}
      </Link>
      </h3>
    </li>
    )
  })
}
</ol>
      </div>
        )
      }
    }

    const stateToProps = (state) => {
      return {
          teams: state.players.teams
      }
    }

    const dispatchToProps = (dispatch) => {
      return {
        selectTeam: (team) => dispatch(actions.selectTeam(team)),
        fetchTeams: (params) => dispatch(actions.fetchTeams(params))
      }
    }

    export default connect(stateToProps, dispatchToProps)(teams)
like image 677
DbTheChain Avatar asked Mar 14 '17 21:03

DbTheChain


2 Answers

You can look at this article :

To Infinity and Beyond with React Waypoint - Infinite Scroll with React Waypoint

Really easy to understand with straight forward approach using react-waypoint.

https://brigade.engineering/to-infinity-and-beyond-with-react-waypoint-cb5ba46a9150#.ftcyb9ynp

Also, one more for your ref : http://brigade.github.io/react-waypoint/#infinite-scroll

Hope this would help :)

like image 176
Ravi Roshan Avatar answered Sep 22 '22 17:09

Ravi Roshan


I was in the same situation a while as the author of this question. Seems like the libraries in the industry generally does not provide a full example of how to integrate this functionality with Redux handling async requests to API.

So I've created a library for the exact same purpose with a full client/server example of the implementation.

You can find the library here: redux-lazy-scroll

You can find the example of Redux implementation part here: https://github.com/shotaK/redux-lazy-scroll/tree/master/dev/client/posts

like image 43
Shota Avatar answered Sep 19 '22 17:09

Shota