1.0.3 • Published 2 years ago

@jdthornton/useinterval v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@jdthornton/useinterval

npm (scoped) npm bundle size (minified)

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>
  )
}