UNPKG

foxact

Version:

React Hooks/Utils done right. For browser, SSR, and React Server Components.

1 lines 524 B
import"client-only";import{useState as e,useRef as t}from"react";import{useEffect as o}from"../use-abortable-effect/index.mjs";function r(r,n){let u=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if("function"==typeof r)throw TypeError("useDebouncedValue does not support function as value");const[i,c]=e(r),l=t(!0);return o(e=>{let t=null;return e.aborted||(u&&l.current?(l.current=!1,c(r)):t=window.setTimeout(()=>{l.current=!0,c(r)},n)),()=>{t&&window.clearTimeout(t)}},[r,u,n]),i}export{r as useDebouncedValue};