logoahooks dive
State

useThrottle

用于处理节流值的 Hook

用法

用来处理节流值的 Hook。

throttledValue:

源码

useThrottle.ts
import { useEffect, useState } from 'react';
import useThrottleFn from '../useThrottleFn';
import type { ThrottleOptions } from './throttleOptions';

function useThrottle<T>(value: T, options?: ThrottleOptions) {
  const [throttled, setThrottled] = useState(value);

  const { run } = useThrottleFn(() => {
    setThrottled(value);
  }, options);

  useEffect(() => {
    run();
  }, [value]);

  return throttled;
}

export default useThrottle;

解读

关于 useThrottleFn,可以查看对应文档:useThrottleFn

function useThrottle<T>(value: T, options?: ThrottleOptions) {
  // 1. 首先,使用 useState 定义了一个 throttled 状态,用于存储节流后的值。
  const [throttled, setThrottled] = useState(value);

  // 2. 然后,使用 useThrottleFn 来定义一个节流函数。
  const { run } = useThrottleFn(() => {
    setThrottled(value);
  }, options);

  // 3. 接着,使用 useEffect 来监听 value 的变化,并调用 run 函数执行节流函数。
  useEffect(() => {
    run();
  }, [value]);

  // 4. 最后,返回节流后的值。
  return throttled;
}

Last updated on

On this page