首页 > 其他分享 >react-infinite-scroll-component 中文文档

react-infinite-scroll-component 中文文档

时间:2022-11-23 22:55:25浏览次数:71  
标签:const component list react infinite import scroll

react-infinite-scroll-component 中文文档

小遁哥 0.1912020.07.18 22:25:10字数 553阅读 16,064

官网地址:https://github.com/ankeetmaini/react-infinite-scroll-component

记得自己曾经弱弱的发问,为什么上拉加载更多会触发多次。

1 上拉加载更多

默认以body/window为容器

import React, { useState, useEffect } from 'react';
import request from '@/utils/request';
import InfiniteScroll from 'react-infinite-scroll-component';
export default () => {
  const [list, setList] = useState([]);
  const requestList = () => {
    setTimeout(async () => {
      const response = await request.get('/api/notes/list', {
        params: {
          name: 12,
        },
      });
      setList(list.concat(response.list));
    }, 1000);
  };
  useEffect(() => {
    requestList();
  }, []);
  return (
    <div>
      <InfiniteScroll
        dataLength={list.length}
        next={requestList}
        hasMore={false}
        endMessage={
          <p style={{ textAlign: 'center' }}>
            <b>Yay! You have seen it all</b>
          </p>
        }
        loader={<h4>Loading...</h4>}
      >
        {list.map((item, index) => (
          <div style={{ height: 100 }} key={index}>
            {item.id}
          </div>
        ))}
      </InfiniteScroll>
    </div>
  );
};

效果如图


 
属性作用注意事项
dataLength 当您上拉时,程序会根据这个值有没有发生变化决定是否触发next 当数据发生变化的间隔非常小时,是会触发多次的,比如快速的把滚动条拖到底
next 用户上拉达到阈值时用于加载更多数据
hasMore 是否还有更多的数据 falsenext不会触发
endMessage 没有更多数据时上拉到底部会显示 需要hasMore 为false
scrollThreshold 上拉时触发next,相当于底部的距离 默认是0.8,您可以设置自己的值,比如200px,则会按照 100%-200px,显然,随着滚动区域越来越高,100%越来越大,200固定不变,意味着越往后您越要上拉的更接近底部才会触发next

在指定区域内触发

只需要使用height

...
      <InfiniteScroll
      ...
        height={400}
      ...
      >
...

效果


 

指定滚动的父容器

需要用到scrollableTarget,这时候InfiniteScroll 就没必要指定height

      <div
        id="scrollableDiv"
        style={{ height: 300, overflow: 'auto' }}
      >
        <InfiniteScroll
          ...
          scrollableTarget="scrollableDiv"
          ...
        >
    ...
      </div>

下拉刷新

import React, { useState, useEffect } from 'react';
import request from '@/utils/request';
import InfiniteScroll from 'react-infinite-scroll-component';
export default () => {
  const [list, setList] = useState([]);
  const requestList = () => {
    setTimeout(async () => {
      const response = await request.get('/api/notes/list', {
        params: {
          name: 12,
        },
      });
      setList(list.concat(response.list));
    }, 1000);
  };
  useEffect(() => {
    requestList();
  }, []);
  return (
    <InfiniteScroll
      dataLength={list.length} //This is important field to render the next data
      next={requestList}
      hasMore={true}
      loader={<h4>Loading...</h4>}
      refreshFunction={requestList}
      pullDownToRefresh
      pullDownToRefreshThreshold={10}
      pullDownToRefreshContent={
        <h3 style={{ textAlign: 'center' }}>
          &#8595; Pull down to refresh
        </h3>
      }
      releaseToRefreshContent={
        <h3 style={{ textAlign: 'center' }}>
          &#8593; Release to refresh
        </h3>
      }
    >
      {list.map((item, index) => (
        <div style={{ height: 100 }} key={index}>
          {item.id}
        </div>
      ))}
    </InfiniteScroll>
  );
};

这个要在移动端看,PC端我在mac笔记本上用 三指下拉也看到过

import React, { useState, useEffect } from 'react';
import request from '@/utils/request';
import InfiniteScroll from 'react-infinite-scroll-component';
export default () => {
  const [list, setList] = useState([]);
  const requestList = () => {
    setTimeout(async () => {
      const response = await request.get('/api/notes/list', {
        params: {
          name: 12,
        },
      });
      setList(list.concat(response.list));
    }, 1000);
  };
  useEffect(() => {
    requestList();
  }, []);
  return (
    <InfiniteScroll
      style={{ marginTop: 120 }}
      height={400}
      dataLength={list.length} //This is important field to render the next data
      next={requestList}
      hasMore={true}
      loader={<h4>Loading...</h4>}
      refreshFunction={requestList}
      pullDownToRefresh
      pullDownToRefreshThreshold={30}
      pullDownToRefreshContent={
        <h3 style={{ textAlign: 'center' }}>
          &#8595; Pull down to refresh
        </h3>
      }
      releaseToRefreshContent={
        <h3 style={{ textAlign: 'center' }}>
          &#8593; Release to refresh
        </h3>
      }
    >
      {list.map((item, index) => (
        <div style={{ height: 100 }} key={index}>
          {item.id}
        </div>
      ))}
    </InfiniteScroll>
  );
};

属性作用注意事项
pullDownToRefresh 是否开启下拉刷新,默认为false  
refreshFunction 触发下拉刷新时调用的函数  
pullDownToRefreshContent 未达到下拉阈值显示的内容
releaseToRefreshContent 达到下拉阈值显示的内容  

效果如下

  Screenshot_20200718_220845_com.chrome.beta.jpg   Screenshot_20200718_220851_com.chrome.beta.jpg

其余的一些属性

nametypedescription
children node (list) the data items which you need to scroll.
onScroll function a function that will listen to the scroll event on the scrolling container. Note that the scroll event is throttled, so you may not receive as many events as you would expect.
className string add any custom class you want
style object any style which you want to override
hasChildren bool children is by default assumed to be of type array and it's length is used to determine if loader needs to be shown or not, if your children is not an array, specify this prop to tell if your items are 0 or more.
initialScrollY number set a scroll y position for the component to render with.
key string the key for the current data set being shown, used when the same component can show different data sets at different times, default=undefined

标签:const,component,list,react,infinite,import,scroll
From: https://www.cnblogs.com/sexintercourse/p/16920450.html

相关文章

  • React 组件通信总结
    React组件通信总结父子通信传递数据(父传子)与传递方法(子传父)/**@Author:[email protected]*@Date:2022-11-2116:02:17*@LastEditors:Hua......
  • Component template should contain exactly one root element
    新建一个页面,出现了这样的错误,刚上手,对vue还不太熟悉,所以对里面的构造方式不太清楚。这个bug,翻译一下:组件模板应该只包含一个根元素  看一下我原来的代码 ......
  • Vue项目网页报错Cannot read property ‘components‘ of undefined
    Vue项目网页报错Cannotreadproperty‘components‘ofundefined   记录一下项目中出现的这个报错,这个报错的原因是在App.vue中导入的组件中重复引用了同一个文......
  • [XState + React] using @xstate/inspect to display state machine char in webapp
    import"./styles.css";importReactfrom"react";importReactDOMfrom"react-dom";import{createMachine,assign}from"xstate";import{useMachine}from......
  • React路由---react-router-dom基本使用
    1.下载react-router-dom5的版本npmireact-router-dom@52.在index.js中从react-router-dom中引入BrowserRouter和HashRouterBrowserRouter使用历史模式history来管......
  • React路由---NavLink与路由重定向
    NavLink当我们希望给当前的路由添加一个样式的时候,可以使用NavLink模块来代替Link模块给NavLink添加activeClassName属性,属性名为class名,在样式中定义active样式App.j......
  • React中性能优化的方案
    减轻state在state中只存储和组件渲染有关的数据不做渲染的数据不放在state中,直接挂载在this上即可,比如定时器的idimport{Component}from"react";​class......
  • React生命周期
    1.什么是生命周期生命周期就是组件从创建到销毁的过程生命周期钩子函数:生命周期的每个阶段总是伴随着一些方法的调用,这些方法就叫生命周期的钩子函数,生命周期的钩子......
  • React Server Components All In One
    ReactServerComponentsAllInOneReactServerComponentshttps://reactjs.org/blog/2020/12/21/data-fetching-with-react-server-components.htmlhttps://github.......
  • React-初始
    最近需要接手别人c#那边组的一个项目新增页面,但他们的是React的框架,作为一名后端,没接触过,一脸懵逼。。。。。。说哈我的处理思路:一、先用相应的程序打开该项......