Skip to content

Instantly share code, notes, and snippets.

@arihantverma
Last active March 14, 2021 11:46
Show Gist options
  • Save arihantverma/08764951a1647c3f9430d074ccf040ab to your computer and use it in GitHub Desktop.
Save arihantverma/08764951a1647c3f9430d074ccf040ab to your computer and use it in GitHub Desktop.
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(() => {
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) {
// implement this
}
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