select滑动分页请求数据

需求背景

Antd 的 select 组件支滑动分页获取后端数据

实现滑动加载数据

定义变量
const allLoadedRef = useRef<boolean>(true); // 是否触底
const [current, setCurrent] = useState<number>(1); // 当前页
const [list, setList] = useState([]); // 列表
定义方法
const getList = async () => {
    try {
      setLoading(true);
      // pageSize 最大 100,让用户感知不到 分页请求数据
      // 调用接口
      // 成功
      // 结构出后端返回给你的 total,赋值
        totalRef.current = total;
        // 10 为 pageSize
        if (current * 10 >= total) {
          allLoadedRef.current = false;
          return;
        }
    } catch {
      message.error('请求超时,请稍后再试!');
    } finally {
      setLoading(false);
    }
  };

监听 current

 useEffect(() => {
    getList();
  }, [current]);

调用

 <Select
  onPopupScroll={(e) => {
  const { target } = e;
  // clientHeight:客户可见的浏览器显示页面的高度。
  // scrollTop:滚动条的滑块距离浏览器页面最顶部的距离,即滚动条滑动了多少距离。
  // scrollHeight:返回元素的完整的高度
  const { clientHeight, scrollTop, scrollHeight } = target as any;
   if (clientHeight + parseInt(scrollTop) === scrollHeight) {
     //表示触底
     if (allLoadedRef.current) setCurrent((op) => op + 1);
   }
 }}
 onChange={onChange}
 >
 //遍历渲染  <Select.Option/>
 </Select>

相关推荐

  1. select滑动请求数据

    2024-02-22 07:40:06       51 阅读
  2. el-select二次封装实现可加载数据

    2024-02-22 07:40:06       61 阅读
  3. 请求存储管理方式

    2024-02-22 07:40:06       22 阅读
  4. 数据库】MySQL查询

    2024-02-22 07:40:06       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-22 07:40:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-22 07:40:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-22 07:40:06       82 阅读
  4. Python语言-面向对象

    2024-02-22 07:40:06       91 阅读

热门阅读

  1. springboot 控制层 aop 日志

    2024-02-22 07:40:06       49 阅读
  2. 深度学习????????

    2024-02-22 07:40:06       53 阅读
  3. BeautifulSoup的使用与入门

    2024-02-22 07:40:06       50 阅读
  4. 计算机科学背后的故事和挑战

    2024-02-22 07:40:06       57 阅读
  5. 设计模式-策略模式

    2024-02-22 07:40:06       50 阅读
  6. 高级统计方法 第1次作业

    2024-02-22 07:40:06       52 阅读
  7. nginx的配置文件详解

    2024-02-22 07:40:06       43 阅读
  8. day38打卡

    2024-02-22 07:40:06       53 阅读
  9. 云计算的两地三中心和灾备介绍

    2024-02-22 07:40:06       53 阅读