因为可以自定义 Hooks, 我们可以非常方便的复用状态逻辑。
// 定时器DEMO function useCount(defaultCount) { const [count, setCount] = useState(defaultCount); const timer = useRef(); useEffect(() => { timer.current = setInterval(() => { setCount((count) => count + 1); }, 1000); }, []); useEffect(() => { if (count >= 10) { clearInterval(timer.current); } }); return [count]; } function App() { const [count] = useCount(0); return ( <> <h1>count: {count}</h1> </> ); }
本文作者:前端小毛
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!