jaredLunde/react-hook

[debounce] Possibility to set state immediately

bluenote10 opened this issue · 1 comments

First of all, awesome library!

Is your feature request related to a problem? Please describe.

A frequent use case of mine is that I want to set a certain state both

  • debounced, when the input comes from the user, i.e., setState is called in an event handler, and
  • immediately, when a state change is not triggered by raw user input, for instance, calling setState to reset a search term value.

Describe the solution you'd like

On first glance, it would be pretty straightforward to include this functionality in useDebounce:

const [value, setValueDebounced, setValueImmediate] = useDebounce(...)

An event handler could now use setValueDebounced while other code may want to use setValueImmediate.

Basically this line would have to return state[1] as third value. For standard usage this should be a non-breaking change, because the array just gets an additional value.

I could prepare a PR if you can fancy the feature.

Describe alternatives you've considered

An alternative design would be to pass the debounce value into each set state call, i.e. setValue("foo", 1000) for debounced, and setValue("foo", 0) for immediate.

However this would be a bigger change, and would probably require to introduce a new hook useVariableDebounce.

I'm open to adding a setValueImmediate callback if you want to PR it. Please include an update to the README if so 😁