项目作者: xynes-hub

项目描述 :
An infinite scroller for react
高级语言: JavaScript
项目地址: git://github.com/xynes-hub/react-scroll-to-fetch.git
创建时间: 2019-04-16T16:56:30Z
项目社区:https://github.com/xynes-hub/react-scroll-to-fetch

开源协议:

下载


react-scroll-to-fetch

Fetch Data from server as you scroll down with a simple lightweight React Component. Saves you all the troubles from designing a pagination system. This lightweight library works both with window scroll event and scrollable div elements.

NPM JavaScript Style Guide

Demo

Install

  1. npm install --save react-scroll-to-fetch

Usage

For Window Scroll

  1. import React, { Component } from 'react'
  2. import ScrollToFetch from 'react-scroll-to-fetch'
  3. class Example extends Component {
  4. render () {
  5. return (
  6. <ScrollToFetch
  7. fetch={this._fetch}
  8. finished={this.state.finished}
  9. initialLoad={true}
  10. loader={<div style={{textAlign:'center'}}>Loading...</div>}
  11. successMessage={<div style={{textAlign:'center'}}>No more data to load</div>}
  12. >{//You dynamic Component that is updated by the fetch function}
  13. </ScrollToFetch>
  14. )
  15. }
  16. }

The _fetch function recieves one parameter , i.e. page and should return a Promise.Or if you declare the _fetch function as async it will work.

An example of the _fetch function

  1. _fetch=(page)=>{
  2. return new Promise((resolve,reject)=>{
  3. axios.get("https://domain.tdl/api/getlist?page="+page)
  4. .then(resp=>{
  5. if(resp.data.success){
  6. //handle data
  7. resolve(resp.data);
  8. }else{
  9. //handle error
  10. reject(resp.data.msg);
  11. }
  12. })
  13. .cathc(err=>{
  14. //handle error
  15. reject(err.message);
  16. }
  17. });
  18. }

An example of the _fetch function using async/await

  1. _fetch=async (page)=>{
  2. try{
  3. const resp=await axios.get("https://domain.tdl/api/getlist?page="+page);
  4. if(resp.data.status){
  5. //handle data
  6. }else{
  7. //handle error
  8. }
  9. }catch(e){
  10. //handle error
  11. }
  12. }

Here is a list of all props :

Name Required Type Default Description
fetch true func A callback to retrive data from the server.
initialLoad true bool A flag to tell ScrollTofetch if it should fetch the first page without scrolling
finished true bool false No more data will be fetched from the server when set to true.
loader false element <div> Loading... </div> A message to show on the bottom of the list. You can replace it with you beautiful loading animation.
successMessage false element <div> No more data to load A message to show when fetching is complete, i.e. finished prop is set to true.
scrollParent false string id of the scrollable div. If you want ScrollToFetch to listen to window scroll events rather than any parent div then do not use this prop.
currentPage false number To manually controll the page no. Click here for more information.
className false string pass className as a prop
style false object pass style as a prop

Manually Controll the Page Number :

Generally if the ScrollToFetch Component gets unmounted, the page no will be reset to zero. Now if you use a global state for the fetched list then there will be problem. To overcome this you can controll the page number with currentPage props. Here is an example.

  1. loadData=async (page)=>{
  2. this.props.setCurrentPage(page);
  3. //fetch data
  4. }
  5. render(){
  6. return (
  7. <ScrollToFetch
  8. fethc={this.loadData}
  9. currentPage={this.props.current_page}
  10. >
  11. {//iterate through the loaded list}
  12. </ScrollToFetch>
  13. )
  14. }

License

MIT © xynes