Repository navigation
feat(useDebouncedState, useThrottledState): add new hook useDebouncedState and useThrottledState - #558
Conversation
🦋 Changeset detectedLatest commit: ac107b9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## v1-base #558 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 71 73 +2
Lines 2288 2346 +58
Branches 735 753 +18
=========================================
+ Hits 2288 2346 +58 🚀 New features to boost your workflow:
|
| setDebouncedValue(() => value); | ||
| } | ||
|
|
||
| const debounced = useDebounce((next: T) => forwardChangedValue(next), debounceMs, { leading, trailing }); |
There was a problem hiding this comment.
Right, I think the direction would be to clean up the reference based deduplication logic in State, Value, and Callback, and move the shared logic from useDebounce into useDebouncedCallback.
One behavioral difference between the two hooks is whether they skip calling the callback if the value is the same as the previously passed value when the debounce fires.
However, having State and Value use Callback could change the existing behavior around initial mounting, updater handling, and so on.
I think I’ll need to work through the implementation to figure out which references to remove and which logic to keep 🤔
(I can’t quite picture it all in my head just yet.)
There was a problem hiding this comment.
As mentioned in the issue, the simplest approach would be to keep it as an internal hook without trying to reuse the public hooks.
|
I'll start working on this once that task is finished! 😎 -> #559 |
hyesungoh
left a comment
There was a problem hiding this comment.
Thank you for your contribution! 👍
Overview
Closes: #538
Adds
useDebouncedStateanduseThrottledState⭐️🙌⭐️Unlike the existing
CallbackandValuehooks, these hooks manage state internally and return the current value along with a debounced or throttled setter, respectively.This lets callers control when state updates are applied without combining a separate
useStatehook.Checklist
yarn run fixto format and lint the code and docs?yarn run test:coverageto make sure there is no uncovered line?