美文网首页
react hooks 中的setInterval的应用

react hooks 中的setInterval的应用

作者: 泽赫 | 来源:发表于2020-10-12 16:47 被阅读0次

    react hooks 中的setInterval的应用

    import React, { useState, useEffect, useRef } from "react";
    import ReactDOM from "react-dom";
    
    function Counter() {
      const [count, setCount] = useState(0);
      const [delay, setDelay] = useState(1000);
      const [isRunning, setIsRunning] = useState(true);
    
      useInterval(() => {
        // Your custom logic here
        setCount(count + 1);
      }, isRunning ? delay : null);
    
      function handleDelayChange(e) {
        setDelay(Number(e.target.value));
      }
    
      function handleIsRunningChange(e) {
        setIsRunning(e.target.checked);
      }
    
      return (
        <>
          <h1>{count}</h1>
          <input type="checkbox" checked={isRunning} onChange={handleIsRunningChange} /> Running
          <br />
          <input value={delay} onChange={handleDelayChange} />
        </>
      );
    }
    
    function useInterval(callback, delay) {
      const savedCallback = useRef();
    
      // Remember the latest function.
      useEffect(() => {
        savedCallback.current = callback;
      }, [callback]);
    
      // Set up the interval.
      useEffect(() => {
        function tick() {
          savedCallback.current();
        }
        if (delay !== null) {
          let id = setInterval(tick, delay);
          return () => clearInterval(id);
        }
      }, [delay]);
    }
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<Counter />, rootElement);
    

    相关文章

      网友评论

          本文标题:react hooks 中的setInterval的应用

          本文链接:https://www.haomeiwen.com/subject/xqhppktx.html