P.Macs
P.Macs

Reputation: 77

how to make an infinite scroller / "Load More" in reactjs

I'm new to reactjs and I'm so confused how to make an infinite scroller or "load more" functionality.

so here is my componentDidMount:

 componentDidMount() {
  $.ajax({
  url:'http://localhost:5118/api/employeedetails/getemployeedetails/',
  success:(data)=>{

    this.setState({
      jsonReturnedValue:data , isLoading: false
    })
  }
})

}

and this where I load my table

renderItem(d, i) {
  return <tr key={i} >
    <td> {d.Employee_ID} </td>
    <td>{d.Employee_Name}</td>
    <td>{d.Address }</td> 
    <td><center><button className ="btn btn-info" onClick={this.handleOnclick.bind(this,  d.Employee_ID, d.Employee_Name, d.Address , d.Department , i)}   data-toggle="modal" data-target="#UpdateEmployee">Edit</button></center></td>
    <td><center><button className ='btn btn-danger'  onClick={this.handleOnclick2.bind (this,d.Employee_ID,d.Employee_Name,i)} data-toggle="modal" data-target="#DeleteEmployee"> Delete</button></center></td>
    </tr>
}

render() {
      if(this.state.isLoading) {
           return <span className="Loader">
                  <h1>
              <span>Loading</span>
              <span className="Loader-ellipsis" >
                <span className="Loader-ellipsisDot">.</span>
                <span className="Loader-ellipsisDot">.</span>
                <span className="Loader-ellipsisDot">.</span>
              </span>
            </h1>
        </span>
     }

    let {jsonReturnedValue} = this.state;
    const isEnabled = this.canBeSubmitted();


  return(
    <div>
    <div>

        <div className="container">   
          <h1> Listof Employees </h1>
            <button className ='btn btn-warning right ' data-toggle="modal" data-target="#AddEmployee"> Add an Employee</button>
             <table className= "table table-bordered" id="result"> 
                <tbody>
                 <tr>
                      <th>ID</th>
                      <th>Name</th>
                      <th>Address</th>
                      <th>Update</th>
                      <th>Delete</th>
                 </tr>
                   {/*  {this.state.tableLoading? <LoadingComponent/>: this.renderItem()}
        */} 
                    {jsonReturnedValue.map((d,i) => this.renderItem(d,i))} 

        </tbody>

            </table>
          </div>

Upvotes: 1

Views: 2697

Answers (2)

Pramod H G
Pramod H G

Reputation: 1613

    componentDidMount() 
  {
    let _this = this;
    $('#notificationMainDiv').on('scroll', function() {
      if($(this).scrollTop() + $(this).innerHeight() >= $(this [0].scrollHeight) 
      {
        if(_this.state.loadMore)
        {
          _this.retrieveNotifications();
        }
      }
    })
  }

Once the bottom of the div is reached, retrieveMoreNotifications() will be called. loadMore will be set to false when no more data to retrieve.

Upvotes: 1

Vibhanshu
Vibhanshu

Reputation: 542

You first need to bind an onScroll eventListener, which you can do in componentDidMount()

componentDidMount() {
 .
 .
 window.addEventListener('scroll', this.onScroll);
}

Then you need to define an onScroll method in your component and check if user has scrolled to the bottom of page:

onScroll = () => {
  // write your code here to check if user has scrolled to the bottom of page
 if(true) { // scrollbar is at the bottom
  this.fetchMoreData();
 }
}

Then define your fetchMoreData function to update the list of jsonReturnedValue.

This is just a blueprint. You need to take care of functionalities by yourself.

To check scrolling refer to this Check if a user has scrolled to the bottom

To get more understanding of event listeners in React refer this https://stackoverflow.com/a/41406239/3323709

Upvotes: 0

Related Questions