import { Dispatch, SetStateAction, useEffect, useState } from "react"; type Props = { initialState: T; delay?: number; }; // this hook is used when you need to reset the state to previous one after a particular time // usecase#1: To make copy to copied and back to copy in clipboard operation export const useTimedReset = ({ delay = 2000, initialState }: Props): [T, boolean, Dispatch>] => { const [state, setState] = useState(initialState); useEffect(() => { let timer: NodeJS.Timeout; if (state !== initialState) { timer = setTimeout(() => setState(initialState), delay); } return () => clearTimeout(timer); }, [state]); // state, isChaning, setState return [state, state !== initialState, setState]; };