You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is probably a bug, but the bug report form didn't fit my report, since I didn't have a test case:
The implementation of useDebounceCallback is defined as this:
exportfunctionuseDebounceCallback<Textends(...args: any)=>ReturnType<T>>(func: T,delay=500,options?: DebounceOptions,): DebouncedState<T>{constdebouncedFunc=useRef<ReturnType<typeofdebounce>>()useUnmount(()=>{if(debouncedFunc.current){debouncedFunc.current.cancel()}})constdebounced=useMemo(()=>{constdebouncedFuncInstance=debounce(func,delay,options)constwrappedFunc: DebouncedState<T>=(...args: Parameters<T>)=>{returndebouncedFuncInstance(...args)}wrappedFunc.cancel=()=>{debouncedFuncInstance.cancel()}wrappedFunc.isPending=()=>{return!!debouncedFunc.current}wrappedFunc.flush=()=>{returndebouncedFuncInstance.flush()}returnwrappedFunc},[func,delay,options])// Update the debounced function ref whenever func, wait, or options changeuseEffect(()=>{debouncedFunc.current=debounce(func,delay,options)},[func,delay,options])returndebounced}
Why is it using both useMemo and useRef/useEffect? The debouncedFunc.current value isn't returned, it's only used in useUnmount to cancel the scheduled debounce function. On the other hand, the result of useMemo is returned, but it is not cancelled on unmount. The observable effect of this is that if func was a setState then it would call it after the component has unmounted.
It seems like there are two implementions here when there should only be one. I guess the useRef/useEffect implementation is more robust.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
This is probably a bug, but the bug report form didn't fit my report, since I didn't have a test case:
The implementation of useDebounceCallback is defined as this:
Why is it using both
useMemo
anduseRef
/useEffect
? ThedebouncedFunc.current
value isn't returned, it's only used inuseUnmount
to cancel the scheduled debounce function. On the other hand, the result ofuseMemo
is returned, but it is not cancelled on unmount. The observable effect of this is that iffunc
was asetState
then it would call it after the component has unmounted.It seems like there are two implementions here when there should only be one. I guess the
useRef
/useEffect
implementation is more robust.Beta Was this translation helpful? Give feedback.
All reactions