@react-hook/timeout
Version:
A React hook that executes a callback after a timeout ms have been exceeded and the timeout has been started
74 lines (61 loc) • 3.61 kB
JavaScript
;
exports.__esModule = true;
exports.useTimeoutCallback = exports.useTimeout = void 0;
var React = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("react"));
var _latest = /*#__PURE__*/_interopRequireDefault( /*#__PURE__*/require("@react-hook/latest"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
* A hook that sets a `timedOut` boolean flag to true after `ms` have passed and the timeout has
* been started.
*
* @param ms The amount of time to wait before setting `timedOut` to `true`
* @returns The first element is a `timedOut` boolean that is `true` when
* the timeout `ms` have been exceeded and `false` otherwise. The second element is a `start` method for
* starting the timeout. The third element is a `reset` method for clearing the timeout.
*
* @example
* const [timedOut, start, reset] = useTimeout(300)
* // starts the timeout
* start()
* // clears the timeout
* reset()
* // logs the timedOut state
* console.log(timedOut)
*/
const useTimeout = (ms = 0) => {
const [timedOut, setTimedOut] = React.useState(false);
const [start, reset] = useTimeoutCallback(() => setTimedOut(true), ms);
return [timedOut, start, reset];
};
/**
* A hook that executes a callback after a timeout is reached and the clock has
* been started.
*
* @param callback The callback you want invoked when the timeout is exceeded
* @param ms The amount of time to wait before firing the callback after `start()` is invoked.
* When this is `0` the callback will never fire.
* @param dependencies Dependencies array for your callback. Anytime your dependencies change,
* the timeout will be reset to an idle state and any pending timeouts will not invoke.
* @returns The first element is a `start` method for starting the timeout. The
* second element is a `reset` method for clearing the timeout.
*
* @example
* const [start, reset] = useTimeoutCallback(() => setState('copied'), 0, [text])
* // starts the timeout
* start()
* // clears the timeout
* reset()
*/
exports.useTimeout = useTimeout;
const useTimeoutCallback = (callback, ms = 0) => {
const [timeout, setTimeoutId] = React.useState();
const storedCallback = (0, _latest.default)(callback); // Clears existing timeouts when a new one set or the hook unmounts
function _ref() {
timeout && clearTimeout(timeout);
}
React.useEffect(() => _ref, [timeout, ms]);
return [React.useCallback(() => setTimeoutId(setTimeout(storedCallback.current, ms)), [ms, storedCallback]), React.useCallback(() => setTimeoutId(undefined), [])];
};
exports.useTimeoutCallback = useTimeoutCallback;