Skip to content

Instantly share code, notes, and snippets.

@luismartinezs
Last active August 30, 2022 08:58
Show Gist options
  • Save luismartinezs/5f06391451660f5fbd5d680f24fe3202 to your computer and use it in GitHub Desktop.
Save luismartinezs/5f06391451660f5fbd5d680f24fe3202 to your computer and use it in GitHub Desktop.
react useInterval #react #hooks
// https://overreacted.io/making-setinterval-declarative-with-react-hooks/
import React, { useState, useEffect, useRef } from "react";
import ReactDOM from "react-dom";
function Counter() {
const [count, setCount] = useState(0);
const [delay, setDelay] = useState(1000);
useInterval(() => {
// Your custom logic here
setCount(count + 1);
}, delay);
function handleDelayChange(e) {
setDelay(Number(e.target.value));
}
return (
<>
<h1>{count}</h1>
<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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment