@jdthornton/useinterval
v1.0.3
Published
React interval hook
Downloads
50
Readme
@jdthornton/useinterval
React interval hook.
Install
$ npm install @jdthornton/useinterval
Usage
import { useState } from 'react';
import useInterval from "@jdthornton/useinterval";
function App(){
const [ isCountActive, setIsCountActive ] = useState();
const [ count, setCount ] = useState(0);
const handleCountToggle = () => setIsCountActive(prevIsCountActive => !prevIsCountActive)
useInterval(
() => setCount(prevCount => prevCount + 1),
isCountActive ? 1000 : null
)
return(
<div>
<div>Count: {count}</div>
<button type="button" onClick={handleCountToggle}>
{isCountActive ? "Stop" : "Start"}
</button>
</div>
)
}