【ProComponents】解决 ProTable 中 params 参数改变,request 函数未触发问题

文章目录

  1. 先建议自查下官方文档,了解paramsrequest直接的关系

在这里插入图片描述

  1. 确定params绑定的参数是否改变,例如 user_name 参数
import { ProTable, WxIcon } from '@/components';
import { getSearchParams } from 'ice';
import { useEffect, useMemo, useRef, useState } from 'react';
import store from '@/store';

export default function (props) {
  const searchParams = getSearchParams();
  const [selectValue, setSelectValue] = useState({
    a: 'user',
    b: undefined,
    c: null,
    user_name: null,
  });
  useEffect(() => {
    setSelectValue({ ...selectValue, user_name: searchParams.user_name});
    // 这里监听 user_name 是否发生了改变
    console.log('user_name = ', searchParams.user_name)
  }, [searchParams.user_name]);

  return (
    <>
      <ProTable
        {/* ...这里做了代码省略,只保留关键属性 */}
        params={{
          ...selectValue,
        }}
        request={async (params) => {
            console.log('参数 =', params);

            const data = await get_list({...省略});
            return {
              data,
              success: true,
              total: data.total,
            };
          }
        }
      />
    </>
  );
}
  1. 如果user_name 参数发生改变,且能正常监听,那导致request 未触发的原因就是:
  • params 更新频率过高,导致 ProTable 来不及及时更新,这时候request 采用节流或防抖就可以了
  • debouncelodash工具库中的防抖函数,也可以通过 js 原生去写防抖
import { ProTable, WxIcon } from '@/components';
import { getSearchParams } from 'ice';
import { useEffect, useMemo, useRef, useState } from 'react';
import store from '@/store';
import { debounce } from 'lodash';

export default function (props) {
  const searchParams = getSearchParams();
  const [selectValue, setSelectValue] = useState({
    a: 'user',
    b: undefined,
    c: null,
    user_name: null,
  });
  useEffect(() => {
    setSelectValue({ ...selectValue, user_name: searchParams.user_name});
    // 这里监听 user_name 是否发生了改变
    console.log('user_name = ', searchParams.user_name)
  }, [searchParams.user_name]);

  return (
    <>
      <ProTable
        {/* ...这里做了代码省略,只保留关键属性 */}
        params={{
          ...selectValue,
        }}
        request={
          debounce(async (params) => {
            console.log('参数 =', params);

            const data = await get_list({...省略});
            return {
              data,
              success: true,
              total: data.total,
            };
          }, 800)
        }
      />
    </>
  );
}

在这里插入图片描述

  • 如果上面方法仍未解决,可以参考下面这个,给params添加useMemo
  • params 提到 useMemo 中,可以避免每次 render 都触发创建一个新 params 对象导致不必要的更新
import { ProTable, WxIcon } from '@/components';
import { getSearchParams } from 'ice';
import { useEffect, useMemo, useRef, useState } from 'react';
import store from '@/store';

export default function (props) {
  const searchParams = getSearchParams();
  const [selectValue, setSelectValue] = useState({
    a: 'user',
    b: undefined,
    c: null,
    user_name: null,
  });
  useEffect(() => {
    setSelectValue({ ...selectValue, user_name: searchParams.user_name});
    // 这里监听 user_name 是否发生了改变
    console.log('user_name = ', searchParams.user_name)
  }, [searchParams.user_name]);
  
  const params = useMemo(()=>({
  	...selectValue,
  }),[selectValue])

  return (
    <>
      <ProTable
        {/* ...这里做了代码省略,只保留关键属性 */}
        params={params}
        request={async (params) => {
            console.log('参数 =', params);

            const data = await get_list({...省略});
            return {
              data,
              success: true,
              total: data.total,
            };
          }
        }
      />
    </>
  );
}

相关推荐

  1. C#参数修饰符params

    2024-03-29 16:36:02       35 阅读
  2. 解决Element-Plusel-switch的change方法自动触发问题

    2024-03-29 16:36:02       343 阅读
  3. 爬虫requests.get参数

    2024-03-29 16:36:02       37 阅读
  4. Django获取request请求参数

    2024-03-29 16:36:02       30 阅读

最近更新

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

    2024-03-29 16:36:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-29 16:36:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-29 16:36:02       87 阅读
  4. Python语言-面向对象

    2024-03-29 16:36:02       96 阅读

热门阅读

  1. Spring 的DI 依赖注入

    2024-03-29 16:36:02       47 阅读
  2. Linux Crontab定时表教学大全(一看就会!)

    2024-03-29 16:36:02       48 阅读
  3. 如何避免过度设计

    2024-03-29 16:36:02       53 阅读
  4. SparkSQL异常数据清洗API

    2024-03-29 16:36:02       48 阅读
  5. CentOS7.x 上安装并配置 MySQL 8.x

    2024-03-29 16:36:02       43 阅读
  6. 小程序配置服务器域名

    2024-03-29 16:36:02       53 阅读
  7. 构建docker环境下的thunder迅雷插件

    2024-03-29 16:36:02       50 阅读