-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dApp: Points - implement claim function debounce (#783)
Closes: #739 There was an issue user was able to click the button multiple times before the modal was displayed causing the claim action to be executed multiple times. it led to multiple points logs registered in the database. As the solution i applied debounce technique to limit calls of the `claimPoints` function.
- Loading branch information
Showing
2 changed files
with
27 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useCallback, useRef } from "react" | ||
|
||
/** | ||
* Debounce a function | ||
* @param callback The function to debounce | ||
* @param delay The delay in ms | ||
* @returns A debounced function | ||
*/ | ||
export default function useDebounce(callback: VoidFunction, delay = 250) { | ||
const latestTimeout = useRef<NodeJS.Timeout>() | ||
|
||
const memoizedCallback = useCallback(callback, [callback]) | ||
|
||
return () => { | ||
if (latestTimeout.current) { | ||
clearTimeout(latestTimeout.current) | ||
} | ||
|
||
latestTimeout.current = setTimeout(() => { | ||
memoizedCallback() | ||
}, delay) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters