react useEffect 里面的计数器,如何在达到指定数字后,停止下来?
这个是官方的一个例子,我改了一下,能够正常计数,从 100 开始倒数。
请问,如何在 count === 0 的时候,停止刷新 /计数?
import React, { useState, useEffect } from "react"; export default function Counter() { const [count, setCount] = useState(100); useEffect(() => { const id = setInterval(() => { setCount((c) => c - 1); }, 1000); return () => clearInterval(id); }, []); return <h1>{count}</h1>; }
refer to: https://react.docschina.org/docs/hooks-faq.html#what-do-hooks-mean-for-popular-apis-like-redux-connect-and-react-router