您的位置:首页 > 文旅 > 美景 > react防抖,解决接口多次请求

react防抖,解决接口多次请求

2024/10/13 6:58:18 来源:https://blog.csdn.net/weixin_54368936/article/details/140776957  浏览:    关键词:react防抖,解决接口多次请求
    useEffect(() => {//  这边走接口 可以获取到totaldebounceSearch()}, [current, pageSize, params])const debounceSearch = useDebounce((e: Event) => {dispatch<scheduleAction>({type: 'schedule/getdata',payload: {dealerId: 3, current: current, pageSize: pageSize, ...params},});}, 800)// 防抖function useDebounce(fn: Function, delay: number) {const refTimer = useRef<number>();return function f(...args: any) {if (refTimer.current) {clearTimeout(refTimer.current);}refTimer.current = setTimeout(() => {fn(args);}, delay);}}

本身react里面的防抖看代码里面别人使用了,照葫芦画瓢没有效果,就扒拉到了这个代码,帮我解决了useEffect的依赖三个参数有一个变化就会触发接口问题,因为我的参数会三个或者两个同事变化,看到接口打印了很多次,请求了很多,影响性能,这个能解决我当下问题,推荐给大家!!!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com