@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
1 lines • 1.23 kB
Source Map (JSON)
{"version":3,"file":"use-throttled-value.cjs","names":["useThrottledCallbackWithClearTimeout"],"sources":["../../src/use-throttled-value/use-throttled-value.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\nimport { useThrottledCallbackWithClearTimeout } from '../use-throttled-callback/use-throttled-callback';\n\nexport function useThrottledValue<T>(value: T, wait: number) {\n const [throttledValue, setThrottledValue] = useState(value);\n const valueRef = useRef(value);\n\n const [throttledSetValue, clearTimeout] = useThrottledCallbackWithClearTimeout(\n setThrottledValue,\n wait\n );\n\n useEffect(() => {\n if (value !== valueRef.current) {\n valueRef.current = value;\n throttledSetValue(value);\n }\n }, [throttledSetValue, value]);\n\n useEffect(() => clearTimeout, []);\n\n return throttledValue;\n}\n"],"mappings":";;;;AAGA,SAAgB,kBAAqB,OAAU,MAAc;CAC3D,MAAM,CAAC,gBAAgB,sBAAA,GAAA,MAAA,UAA8B,MAAM;CAC3D,MAAM,YAAA,GAAA,MAAA,QAAkB,MAAM;CAE9B,MAAM,CAAC,mBAAmB,gBAAgBA,+BAAAA,qCACxC,mBACA,KACD;AAED,EAAA,GAAA,MAAA,iBAAgB;AACd,MAAI,UAAU,SAAS,SAAS;AAC9B,YAAS,UAAU;AACnB,qBAAkB,MAAM;;IAEzB,CAAC,mBAAmB,MAAM,CAAC;AAE9B,EAAA,GAAA,MAAA,iBAAgB,cAAc,EAAE,CAAC;AAEjC,QAAO"}