-
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?
Conversation
…nce, also added useDebouncedValue
|
} | ||
|
||
|
||
export {useDebounce as default, useDebouncedValue} |
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've exported as default here, as it's the current behaviour.
@@ -20,4 +20,21 @@ const useDebounce = <T extends (params: any) => any>( | |||
return { debouncedFunction: debouncedFuncRef.current } |
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 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 comment
The 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
const useDebounce = <T extends (params: any) => any>( | ||
functionToDebounce: T, | ||
const useDebounce = <T extends any[], U>( | ||
functionToDebounce: (...params: T) => U, |
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
const useDebounce = <T extends (...params: any[]) => any>(
functionToDebounce: T,
{ debounceTime = 400 } = {},
) => ...
I just added it as it's how I'd personally write this generic, but I think it's the same thing functionally.
@@ -20,4 +20,21 @@ const useDebounce = <T extends (params: any) => any>( | |||
return { debouncedFunction: debouncedFuncRef.current } |
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 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
@@ -20,4 +20,21 @@ const useDebounce = <T extends (params: any) => any>( | |||
return { debouncedFunction: debouncedFuncRef.current } | |||
} | |||
|
|||
export default useDebounce | |||
const useDebouncedValue = <T>(value: T, debounceTime: number) => { |
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.
maybe we move this to its own file?
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.
Yup I can do that 👍
Quality Gate passedIssues Measures |
Updated the
useDebounce
to allow for more than one parameter.useDebouncedValue lets you debounce a value instead of function, something like
this lets the UI update immidietly be tracking
searchValue
but thedebouncedSearchValue
only updated after 150ms (and only once per 150ms period)@deboracosilveira I believe you originally wrote the
useDebouncedValue
for WAYS 😄