倒计时reacthooks

273 阅读1分钟
const [count, setCount] = useState(60);
...
useEffect(() => {
    setTimeout(() => {
      if (count > 0) {
         setCount((num) => num - 1);
      }
    }, 1000);
  }, [count]);

...
<Button onClick={() => setCount(10)}>dianwo</Button>