Skip to content

Commit

Permalink
fix: debounce only when has value
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Jun 1, 2022
1 parent 8531863 commit 0a18dc2
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/widget/src/hooks/useDebouncedWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ export const useDebouncedWatch = (name: any, delay: number) => {

useEffect(() => {
if (isMounted.current) {
const handler = setTimeout(() => {
setDebouncedValue(watchedValue);
}, delay);
return () => clearTimeout(handler);
const hasWatchedValue = Array.isArray(watchedValue)
? watchedValue.some((value) => value)
: watchedValue;
if (hasWatchedValue) {
const handler = setTimeout(() => {
setDebouncedValue(watchedValue);
}, delay);
return () => clearTimeout(handler);
}
setDebouncedValue(watchedValue);
return undefined;
}
isMounted.current = true;
return undefined;
Expand Down

0 comments on commit 0a18dc2

Please sign in to comment.