Skip to main content

useCountDown

React State Hooks that return the minutes gracefull

Usage

Live Editor

function Demo() {
  const now = new Date();
  const tomorrow = new Date();
  tomorrow.setDate(now.getDate() + 1);
  tomorrow.setHours(0, 0, 0, 0);
  const diffInSec = Math.floor((tomorrow.getTime() - now.getTime()) / 1000);

  // note: If your app is running in server side, must pass the same time as the client
  // this demo is not running in server side
  const [hour, minute, second] = useCountDown(diffInSec);
  return (
    <div suppressHydrationWarning={true}>{`${hour}:${minute}:${second}`}</div>
  );
};

Result
Loading...

API

useCountdown

Returns

readonly [string, string, string]: A tuple with the following elements:

  • hour
  • minute.
  • second.

Arguments

ArgumentDescriptionTypeDefaultValue
timetime differnumber (Required)-
formattime format function((num: number) => [string, string, string]) | undefinedHH MM SS
callbackcallback function for end of countdown(() => void) | undefined-