hooks icon indicating copy to clipboard operation
hooks copied to clipboard

useCountDown中的leftTime并没有正确支持动态设置

Open baixiaoyu2997 opened this issue 11 months ago • 2 comments

const [targetDate, setTargetDate] = useState<number>();
  const [countDown] = useCountDown({
    targetDate,
  });
  const rotatingTime = 1;
  const onResend = () => {
    if (countDown !== 0) return;
    setTargetDate(Date.now() + 10 * 1000);
    setIsRotating(true);
    setTimeout(() => {
      setIsRotating(false);
    }, rotatingTime * 1000);
    props.onResend?.();
  };

这样是可以的,但是改为leftTime就不生效了:

const [leftTime, setLeftTime] = useState();
  const [countDown] = useCountDown({ leftTime });
  const rotatingTime = 1;
  const onResend = () => {
    if (countDown !== 0) return;
    setLeftTime(10 * 1000);
    setIsRotating(true);
    setTimeout(() => {
      setIsRotating(false);
    }, rotatingTime * 1000);
    props.onResend?.();
  };

version: 3.8.4

baixiaoyu2997 avatar Jan 09 '25 08:01 baixiaoyu2997

@crazylxr 看了下useEffect里面拿target做依赖,leftTime没变化的时候不会重启计时,后续感觉是不是可以像vueuse那样多返回一些操作函数,比如reset重置计时

Image

Arktomson avatar Sep 03 '25 01:09 Arktomson

@crazylxr 看了下useEffect里面拿target做依赖,leftTime没变化的时候不会重启计时,后续感觉是不是可以像vueuse那样多返回一些操作函数,比如reset重置计时

Image

来个 pr ?

meet-student avatar Oct 23 '25 15:10 meet-student