• raect hook中使用防抖(debounce)和节流(throttle)


    一、基础

      参考以前写的博客:https://www.cnblogs.com/gg-qq/p/11249200.html

    二、react中实现防抖和节流的hooks

      手写可靠的useDebounce:

    export function useDebounce(fn, delay) {
      const { current } = useRef({});
      function f(...args) {
        if (current.timer) {
          clearTimeout(current.timer);
        }
        current.timer = setTimeout(fn.bind(undefined, ...args), delay);
      }
      return f;
    }

      useThrottle:

    export function useThrottle(fn, delay) {
      const { current } = useRef({});
      function f(...args) {
        if (!current.timer) {
          current.timer = setTimeout(() => {
            delete current.timer;
          }, delay);
          fn(...args);
        }
      }
      return f;
    }

     三、创建唯一的debounce,防止重复调用函数

      使用useCallback由于创建之后useCallback从不刷新,它内部的state一直不变 (内部进行setter会改变外部state)

    const load = useCallback(
        debounce(() => loadList(), 500),
        [],
      );

      使用UseRef,保存函数,终身存在

    const load = useRef(
        debounce(() => loadList(), 500),
        [],
      );

     

  • 相关阅读:
    Nginx日志
    Aapche日志
    IIS日志
    pikachu 不安全的url重定向
    pikachu php反序列化、XXE、SSRF
    pikachu 越权漏洞
    pikachu 不安全的文件下载和上传
    pikachu Files Inclusion(文件包含漏洞)
    pikachu RCE部分(远程命令、代码执行漏洞)
    pikachu SQL部分(下)
  • 原文地址:https://www.cnblogs.com/gg-qq/p/15338952.html
Copyright © 2020-2023  润新知