React hook exposing a countdown timer with optional expiration and reset callbacks
npm install use-countdown-timer> React hook exposing a countdown timer with optional expiration and reset callbacks





``bash`
npm install --save use-countdown-timer
use-countdown-timer is written in TypeScript and bundles type definitions.
`ts
export interface ICountdownTimerParams {
timer: number;
interval?: number;
autostart?: boolean;
expireImmediate?: boolean;
onExpire?: () => void;
onReset?: () => void;
}
export declare type CountdownTimerResults = {
countdown: number;
isRunning: boolean;
start: () => void;
reset: () => void;
pause: () => void;
};
`
`tsx
import React from 'react';
import { useCountdownTimer } from 'use-countdown-timer';
const Example = () => {
const { countdown, start, reset, pause, isRunning } = useCountdownTimer({
timer: 1000 * 5,
});
return (
{countdown}
{isRunning ? (
) : (
)}
);
};
`
Local development involves two parts.
First, install and start the library so that it watches src/ directory and automatically recompiles to dist/ on change.
`bashclone the repo
git clone https://github.com/LobsterBandit/use-countdown-timer.git
Second, install and start the example app that is linked to your local
use-countdown-timer version.`bash
in second terminal window
cd example/
npm installstart create-react-app dev server
npm start
`Now, any changes to
src/ in the local use-countdown-timer or to the example app's example/src/` will live-reload the dev server.MIT © LobsterBandit
---
This hook is created using create-react-hook.