Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nick-prat
Copy link

Updated the useDebounce to allow for more than one parameter.

useDebouncedValue lets you debounce a value instead of function, something like

const [searchValue, setSearchValue] = useState("")
const debouncedSearchValue = useDebouncedValue(searchValue, 150)

this lets the UI update immidietly be tracking searchValue but the debouncedSearchValue only updated after 150ms (and only once per 150ms period)

@deboracosilveira I believe you originally wrote the useDebouncedValue for WAYS 😄

@nick-prat nick-prat added the enhancement New feature or request label Feb 27, 2024
Copy link

changeset-bot bot commented Feb 27, 2024

⚠️ No Changeset found

Latest commit: 5ef868e

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

}


export {useDebounce as default, useDebouncedValue}
Copy link
Author

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 }
Copy link
Author

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?

Copy link
Collaborator

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,
Copy link
Collaborator

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?

Copy link
Author

@nick-prat nick-prat Feb 27, 2024

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 }
Copy link
Collaborator

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) => {
Copy link
Collaborator

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?

Copy link
Author

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 👍

Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

2 participants