index.jsx 886B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import React, { useEffect, useRef, useState } from 'react'
  2. import ReactList from 'react-list'
  3. import './index.scss'
  4. export default props => {
  5. const ref = useRef()
  6. const handleScroll = () => {
  7. const done = props.length >= props.total
  8. if (!done) {
  9. const [, last] = ref.current.getVisibleRange()
  10. // 如果未展示的数据少于3条就请求数据
  11. if (props.length - last < 3) {
  12. props.loadMore()
  13. }
  14. }
  15. }
  16. const NoData = (
  17. <div className="no-data">
  18. {props.nodata || '暂无数据'}
  19. </div>
  20. )
  21. return (
  22. <div className="inifinite-list-wrapper" style={{height: props.height}} onScroll={handleScroll}>
  23. {
  24. !props.length ? NoData :
  25. <ReactList
  26. ref={ref}
  27. type="uniform"
  28. length={props.length}
  29. itemRenderer={props.itemRenderer}
  30. />
  31. }
  32. </div>
  33. )
  34. }