use-debounce-hook-web 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
Binary file
@@ -0,0 +1,3 @@
1
+ {
2
+ "presets": ["@babel/preset-env", "@babel/preset-react"]
3
+ }
Binary file
Binary file
Binary file
Binary file
Binary file
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "use-debounce-hook-web",
3
+ "version": "1.0.0",
4
+ "description": "A simple and reusable debounce hook for React",
5
+ "main": "src/index.js",
6
+ "keywords": [
7
+ "react",
8
+ "debounce",
9
+ "hook",
10
+ "react-hooks",
11
+ "useDebounce"
12
+ ],
13
+ "author": "Yogesh Negi",
14
+ "license": "MIT",
15
+ "peerDependencies": {
16
+ "react": ">=16.8.0"
17
+ },
18
+ "scripts": {
19
+ "test": "echo \"No tests yet\""
20
+ },
21
+ "devDependencies": {
22
+ "@babel/core": "^7.28.5",
23
+ "@babel/preset-env": "^7.28.5",
24
+ "@babel/preset-react": "^7.28.5"
25
+ }
26
+ }
package/src/index.js ADDED
@@ -0,0 +1 @@
1
+ export { default as useDebounce } from "./useDebounce";
@@ -0,0 +1,47 @@
1
+ import { useEffect, useRef, useState } from "react";
2
+
3
+ const useDebounce = (
4
+ value,
5
+ delay = 500,
6
+ options = { immediate: false, maxWait: null }
7
+ ) => {
8
+ const [debouncedValue, setDebouncedValue] = useState(value);
9
+
10
+ const timeoutRef = useRef(null);
11
+ const maxWaitRef = useRef(null);
12
+ const firstRender = useRef(true);
13
+
14
+ useEffect(() => {
15
+ // Immediate execution (first call)
16
+ if (options.immediate && firstRender.current) {
17
+ setDebouncedValue(value);
18
+ firstRender.current = false;
19
+ return;
20
+ }
21
+
22
+ // Clear previous timers
23
+ if (timeoutRef.current) clearTimeout(timeoutRef.current);
24
+ if (maxWaitRef.current) clearTimeout(maxWaitRef.current);
25
+
26
+ // Main debounce timer
27
+ timeoutRef.current = setTimeout(() => {
28
+ setDebouncedValue(value);
29
+ }, delay);
30
+
31
+ // Max wait support
32
+ if (options.maxWait) {
33
+ maxWaitRef.current = setTimeout(() => {
34
+ setDebouncedValue(value);
35
+ }, options.maxWait);
36
+ }
37
+
38
+ return () => {
39
+ clearTimeout(timeoutRef.current);
40
+ clearTimeout(maxWaitRef.current);
41
+ };
42
+ }, [value, delay, options.immediate, options.maxWait]);
43
+
44
+ return debouncedValue;
45
+ };
46
+
47
+ export default useDebounce;