-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add multi-param support to useDebounce, add useDebouncedValue #87
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
'use client' | ||
|
||
import { useEffect, useRef } from 'react' | ||
import { useEffect, useState, useRef } from 'react' | ||
|
||
import debounce from 'lodash/debounce' | ||
|
||
const useDebounce = <T extends (params: any) => any>( | ||
functionToDebounce: T, | ||
const useDebounce = <T extends any[], U>( | ||
functionToDebounce: (...params: T) => U, | ||
{ debounceTime = 400 } = {}, | ||
) => { | ||
const debouncedFuncRef = useRef(debounce(functionToDebounce, debounceTime, { trailing: true })) | ||
|
@@ -20,4 +20,21 @@ | |
return { debouncedFunction: debouncedFuncRef.current } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also wanted to ask why we return this as an object instead of just the value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there was a real reason for that at the time, I guess because we were thinking about returning more data. I'm ok to return only the function instead |
||
} | ||
|
||
export default useDebounce | ||
const useDebouncedValue = <T>(value: T, debounceTime: number) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we move this to its own file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup I can do that 👍 |
||
const [debouncedValue, setDebouncedValue] = useState(value) | ||
|
||
useEffect(() => { | ||
const handler = setTimeout(() => { | ||
setDebouncedValue(value) | ||
}, debounceTime) | ||
|
||
return () => { | ||
clearTimeout(handler) | ||
} | ||
}, [value, debounceTime]) | ||
|
||
return debouncedValue | ||
} | ||
|
||
|
||
export {useDebounce as default, useDebouncedValue} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've exported as default here, as it's the current behaviour. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why we would need to manually define a return type here, could you explain?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this could have been
I just added it as it's how I'd personally write this generic, but I think it's the same thing functionally.