项目作者: mdubourg001

项目描述 :
A React handy component to render infinite, onScroll fetched, data lists.
高级语言: JavaScript
项目地址: git://github.com/mdubourg001/react-infinite-list.git
创建时间: 2019-02-24T23:51:04Z
项目社区:https://github.com/mdubourg001/react-infinite-list

开源协议:MIT License

下载


react-infinite-list

A handy React component to render infinite, onScroll fetched, data lists.

Demo: https://react-infinite-list.netlify.com

GitHub
@damnhotuser/react-infinite-list.svg" alt="npm">


Install

  1. $ npm install @damnhotuser/react-infinite-list

Usage

react-infinite-list provides a single component, InfiniteList.

  1. import React, { useState } from "react";
  2. import InfiniteList from "@damnhotuser/react-infinite-list";
  3. import mock_fetch from "./mock_data"; // mocking an API service
  4. const App = props => {
  5. const [rows, setRows] = useState([]);
  6. const fetchData = (offset, limit) =>
  7. mock_fetch(offset, limit).then(data => {
  8. setRows([...rows, ...data]);
  9. });
  10. return (
  11. /*
  12. * InfiniteList takes three needed arguments:
  13. * `rows` are data to display in the list
  14. * `fetchData` is called on rendering and when needed, on scroll
  15. * `limit` is the number of rows to fetch on each `fetchData` call
  16. *
  17. * InfiniteList's `children` must be a function, and has the row to render passed as an argument
  18. *
  19. * current `index` and `ref` will also be passed as arguments of your `children` function, it is IMPORTANT to pass ref to the rendered list element for the onScroll fetch to work
  20. */
  21. <InfiniteList rows={rows} fetchData={fetchData} limit={limit}>
  22. {(row, index, ref) => (
  23. <div key={index} ref={ref}>
  24. {row.name}
  25. </div>
  26. )}
  27. </InfiniteList>
  28. );
  29. };


⚠️ InfiniteList’s children must be a function. The current row to render will be passed as an argument.

⚠️ current index and ref will also be passed as arguments of your children function, it is IMPORTANT to pass ref to the rendered list element for the onScroll fetch to work


InfiniteList takes also 3 optionnal arguments:

  • containerClasses are classes that will be passed as an argument to the div holding your list.
  • containerStyle are style rules that will be passed as an argument to the div holding your list.
  • fetchThreshold (number) is the number of element before the end of the displayed list to wait before calling fetchData again. i.e. if n elements are displayed on the list, and fetchThreshold is set to 3,fetchData will be called when the n-3th element of the list is scrolled into view. The default value of fetchthreshold is 5.