debounce watch #3078
Replies: 5 comments 7 replies
-
Not possible.
Cheers
Bill
On 5 Oct 2020, at 11:44 am, Rebecca Stevens <[email protected]<mailto:[email protected]>> wrote:
Is it possible to debounce the re-render caused by watch?
(This is probably isn't specific to this lib and is just about debouncing re-renders in general).
I made a sandbox that shows a react element wrapping the input where the element needs to re-render with every change. It would be nice if I could debounce re-rendering of it though.
https://codesandbox.io/s/broken-snowflake-k98fz?file=/src/Textbox.tsx
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub<#3078>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ACQGXVDBV5HV3KXKDGKPY4DSJEJHJANCNFSM4SEBTG3Q>.
|
Beta Was this translation helpful? Give feedback.
-
Hi, all. I know this is a stale issue, but I recently was looking for a way to debounce the watched value of a field and there seemed to not be much literature about this. My use case was a search input where some network requests would be made after searching. I wanted to debounce this without A: debouncing the actual form input value (so the typing feels normal) or B: debouncing the network call. Perhaps either of these options or some other might present an alternative path. But it seemed to me that there is in fact a way to debounce rerenders. Since this is the first question I came across asking how, I'll respond to the OP although this question is no doubt stale by now. Using a child component that renders nothing we can pass a Here is a fork of the OP's sandbox with a POC. Cheers |
Beta Was this translation helpful? Give feedback.
-
FYI here's my adaptation of @aylabbs's example: import { Fragment, ReactElement, useEffect } from 'react'
import { Control, useWatch } from 'react-hook-form'
interface IProps {
value: any
set: (n: any) => void
control: Control
name: string
duration?: number
}
export default function Debounce ({ value, set, control, name, duration }: IProps): ReactElement {
const internal = useWatch({ control, name })
useEffect(() => {
const timeout = setTimeout(() => {
if (value !== internal) {
set(internal)
}
}, duration ?? 500)
return () => {
clearTimeout(timeout)
}
}, [value, set, internal, duration])
return <Fragment />
} It's definitely not perfect, it'd be great to have native support for this, but hopefully this'll help others searching. |
Beta Was this translation helpful? Give feedback.
-
it works for me:
|
Beta Was this translation helpful? Give feedback.
-
Mine function useDebouncedValue<TValue>(value: TValue, delay: number) {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(handler);
};
}, [value, delay]);
return debouncedValue;
}
//
const videoId = useWatch({ name: "youtubeUrl", compute: parseVideoIdFromYoutubeUrl });
const debouncedVideoId = useDebouncedValue(videoId, 300); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Is it possible to debounce the re-render caused by
watch
?(This is probably isn't specific to this lib and is just about debouncing re-renders in general).
I made a sandbox that shows a react element wrapping the input where the element needs to re-render with every change. It would be nice if I could debounce re-rendering of it though.
https://codesandbox.io/s/broken-snowflake-k98fz?file=/src/Textbox.tsx
Beta Was this translation helpful? Give feedback.
All reactions