hookagain

hookagain

Hooks

useInterval

A hook that provides a way to execute a callback function at specified intervals.

Installation

npx shadcn@latest add https://hookagain.vercel.app/r/use-interval.json

Usage

import { useInterval } from "@/hooks/use-interval"
 
function Example() {
  const [count, setCount] = React.useState(0)
  const [isRunning, setIsRunning] = React.useState(false)
 
  useInterval(
    () => {
      setCount((count) => count + 1)
    },
    // Pass null to pause the interval
    isRunning ? 1000 : null
  )
 
  return (
    <div className="flex flex-col gap-4">
      <p>Count: {count}</p>
      <button
        onClick={() => setIsRunning(!isRunning)}
        className="px-4 py-2 border rounded"
      >
        {isRunning ? "Stop" : "Start"} Counter
      </button>
    </div>
  )
}

On this page