UNPKG

@react-hook/timeout

Version:

A React hook that executes a callback after a timeout ms have been exceeded and the timeout has been started

1 lines 3.18 kB
{"version":3,"file":"index.mjs","sources":["../../src/index.tsx"],"sourcesContent":["import * as React from 'react'\nimport useLatest from '@react-hook/latest'\n\n/**\n * A hook that sets a `timedOut` boolean flag to true after `ms` have passed and the timeout has\n * been started.\n *\n * @param ms The amount of time to wait before setting `timedOut` to `true`\n * @returns The first element is a `timedOut` boolean that is `true` when\n * the timeout `ms` have been exceeded and `false` otherwise. The second element is a `start` method for\n * starting the timeout. The third element is a `reset` method for clearing the timeout.\n *\n * @example\n * const [timedOut, start, reset] = useTimeout(300)\n * // starts the timeout\n * start()\n * // clears the timeout\n * reset()\n * // logs the timedOut state\n * console.log(timedOut)\n */\nexport const useTimeout = (ms = 0): [boolean, () => void, () => void] => {\n const [timedOut, setTimedOut] = React.useState(false)\n const [start, reset] = useTimeoutCallback(() => setTimedOut(true), ms)\n return [timedOut, start, reset]\n}\n\n/**\n * A hook that executes a callback after a timeout is reached and the clock has\n * been started.\n *\n * @param callback The callback you want invoked when the timeout is exceeded\n * @param ms The amount of time to wait before firing the callback after `start()` is invoked.\n * When this is `0` the callback will never fire.\n * @param dependencies Dependencies array for your callback. Anytime your dependencies change,\n * the timeout will be reset to an idle state and any pending timeouts will not invoke.\n * @returns The first element is a `start` method for starting the timeout. The\n * second element is a `reset` method for clearing the timeout.\n *\n * @example\n * const [start, reset] = useTimeoutCallback(() => setState('copied'), 0, [text])\n * // starts the timeout\n * start()\n * // clears the timeout\n * reset()\n */\nexport const useTimeoutCallback = (\n callback: (...args: any[]) => any,\n ms = 0\n): [() => void, () => void] => {\n const [timeout, setTimeoutId] = React.useState<\n ReturnType<typeof setTimeout>\n >()\n const storedCallback = useLatest(callback)\n // Clears existing timeouts when a new one set or the hook unmounts\n React.useEffect(\n () => () => {\n timeout && clearTimeout(timeout)\n },\n [timeout, ms]\n )\n\n return [\n React.useCallback(\n () => setTimeoutId(setTimeout(storedCallback.current, ms)),\n [ms, storedCallback]\n ),\n React.useCallback(() => setTimeoutId(undefined), []),\n ]\n}\n"],"names":["useTimeout","ms","timedOut","setTimedOut","React","start","reset","useTimeoutCallback","callback","timeout","clearTimeout","setTimeoutId","storedCallback","useLatest","setTimeout","current","undefined"],"mappings":"uGAqBaA,EAAa,SAACC,YAAAA,IAAAA,EAAK,OACvBC,EAAUC,GAAeC,EAAe,IACxCC,EAAOC,GAASC,EAAmB,IAAMJ,EAAY,GAAOF,SAC5D,CAACC,EAAUG,EAAOC,IAsBdC,EAAqB,SAChCC,EACAP,gBASIQ,GAAWC,aAAaD,YAT5BR,IAAAA,EAAK,OAEEQ,EAASE,GAAgBP,IAG1BQ,EAAiBC,EAAUL,UAEjCJ,EACE,MAGA,CAACK,EAASR,IAGL,CACLG,EACE,IAAMO,EAAaG,WAAWF,EAAeG,QAASd,IACtD,CAACA,EAAIW,IAEPR,EAAkB,IAAMO,OAAaK,GAAY"}