Skip to content

Latest commit

 

History

History
11 lines (11 loc) · 360 Bytes

useDebounce.md

File metadata and controls

11 lines (11 loc) · 360 Bytes
const useDebounce = <T>(value: T, delay?: number) => {
  const [debounceValue, setDebounceValue] = useState(value);
  useEffect(() => {
    const timeout = setTimeout(() => setDebounceValue(value), delay);
    // 再上一个 useEffect 处理完之后再运行
    return () => clearTimeout(timeout);
  }, [value, delay]);
  return debounceValue;
};