相关疑难解决方法(0)

React 中的去抖动和超时

我这里有一个输入字段,在每种类型上,它都会调度一个 redux 操作。我放置了一个 useDebounce 以便它不会很重。问题是它说Hooks can only be called inside of the body of a function component.What is the theproper way to do it?

使用超时

import { useCallback, useEffect, useRef } from "react";

export default function useTimeout(callback, delay) {
  const callbackRef = useRef(callback);
  const timeoutRef = useRef();

  useEffect(() => {
    callbackRef.current = callback;
  }, [callback]);

  const set = useCallback(() => {
    timeoutRef.current = setTimeout(() => callbackRef.current(), delay);
  }, [delay]);

  const clear = useCallback(() => {
    timeoutRef.current && clearTimeout(timeoutRef.current); …
Run Code Online (Sandbox Code Playgroud)

javascript debouncing ecmascript-6 reactjs react-hooks

7
推荐指数
1
解决办法
1万
查看次数