📌  相关文章
📜  带有反应钩子的计数器 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:05.253000             🧑  作者: Mango

代码示例2
const { useState, useEffect, useRef } = React;

function useInterval(callback, delay) {
  const savedCallback = useRef();

  // Remember the latest callback.
  useEffect(() => {
    savedCallback.current = callback;
  }, [callback]);

  // Set up the interval.
  useEffect(() => {
    let id = setInterval(() => {
      savedCallback.current();
    }, delay);
    return () => clearInterval(id);
  }, [delay]);
}

function App() {
  const [counter, setCounter] = useState(0);

  useInterval(() => {
    setCounter(counter + 1);
  }, 1000);

  return 

{counter}

; }; ReactDOM.render( , document.getElementById('root') );