@carbon/react
Version:
React components for the Carbon Design System
41 lines (39 loc) • 1.17 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
require("../_virtual/_rolldown/runtime.js");
let react = require("react");
//#region src/internal/useDelayedState.ts
/**
* Copyright IBM Corp. 2016, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
function useDelayedState(initialState) {
const [state, setState] = (0, react.useState)(initialState);
const timeoutId = (0, react.useRef)(null);
const setStateWithDelay = (0, react.useCallback)((stateToSet, delayMs = 0) => {
window.clearTimeout(timeoutId.current ?? void 0);
timeoutId.current = null;
if (delayMs === 0) {
setState(stateToSet);
return;
}
timeoutId.current = window.setTimeout(() => {
setState(stateToSet);
timeoutId.current = null;
}, delayMs);
}, []);
(0, react.useEffect)(() => {
return () => {
window.clearTimeout(timeoutId.current ?? void 0);
};
}, []);
return [state, setStateWithDelay];
}
//#endregion
exports.useDelayedState = useDelayedState;