Docs
useTimeout

Run a callback function after a specified delay with useTimeout, supporting dynamic updates and cleanup on unmount.

Installation

pnpm dlx shadcn@latest add https://hookcn.ouassim.tech/r/use-timeout

Usage

"use client"
 
import * as React from "react"
 
import { useTimeout } from "@/hooks/use-timeout"
 
export function Component() {
  const [visible, setVisible] = React.useState(true)
 
  const hide = () => {
    setVisible(false)
  }
 
  useTimeout(hide, 5000)
 
  return (
    <div>
      <p>
        {visible
          ? "I'm visible for 5000ms"
          : "You can no longer see this content"}
      </p>
    </div>
  )
}

API Reference

Parameters

NameTypeDescription
callback() => voidThe function to be executed when the timeout elapses.
delaynull | numberThe duration (in milliseconds) for the timeout. Set to null to clear the timeout.

Returns

void