From 8aa7f9b7dffe0974417abf032e6b90e45e73e3ed Mon Sep 17 00:00:00 2001 From: edelgarat Date: Tue, 30 Jan 2024 15:17:01 +0300 Subject: [PATCH] add new hook --- package-lock.json | 4 ++-- package.json | 2 +- src/hooks/index.ts | 1 + src/hooks/useDebouncedValue.ts | 10 ++++++++++ 4 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 src/hooks/useDebouncedValue.ts diff --git a/package-lock.json b/package-lock.json index 1ae81ed..dab0078 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@worksolutions/react-utils", - "version": "2.2.1", + "version": "2.2.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@worksolutions/react-utils", - "version": "2.2.1", + "version": "2.2.2", "dependencies": { "@popperjs/core": "^2.*", "@worksolutions/utils": "^1.4.3", diff --git a/package.json b/package.json index 6920c4b..a7f0918 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@worksolutions/react-utils", "private": false, - "version": "2.2.1", + "version": "2.2.2", "description": "", "types": "dist/esm/index.d.ts", "main": "dist/cjs/index.js", diff --git a/src/hooks/index.ts b/src/hooks/index.ts index 176c699..ba3faf9 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -6,6 +6,7 @@ export * from "./special"; export * from "./useAsyncFn"; export * from "./useChildrenMeasure"; export * from "./useDebouncedInput"; +export * from "./useDebouncedValue"; export * from "./useMeasure"; export * from "./useTimer"; export * from "./useStickyEffectDetector"; diff --git a/src/hooks/useDebouncedValue.ts b/src/hooks/useDebouncedValue.ts new file mode 100644 index 0000000..8839397 --- /dev/null +++ b/src/hooks/useDebouncedValue.ts @@ -0,0 +1,10 @@ +import React, { useState } from "react"; + +import { useDebounceRef } from "./common"; + +export function useDebouncedValue(value: VALUE, debounceTime: number) { + const [resultValue, setResultValue] = useState(value); + const handleDebouncedChangeRef = useDebounceRef(debounceTime, setResultValue); + React.useEffect(() => handleDebouncedChangeRef.current(value), [handleDebouncedChangeRef, value]); + return resultValue; +}