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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions packages/utils/hooks/useDebounce/index.ts
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,
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.

{ debounceTime = 400 } = {},
) => {
const debouncedFuncRef = useRef(debounce(functionToDebounce, debounceTime, { trailing: true }))
Expand All @@ -20,4 +20,21 @@
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

}

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 👍

const [debouncedValue, setDebouncedValue] = useState(value)

useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value)
}, debounceTime)

return () => {
clearTimeout(handler)
}
}, [value, debounceTime])

return debouncedValue
}


export {useDebounce as default, useDebouncedValue}

Check failure on line 40 in packages/utils/hooks/useDebounce/index.ts

View workflow job for this annotation

GitHub Actions / Lint

'default' is restricted from being used as an exported name
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.

Loading