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
functionuseThrottle<T>(value: T,options?: ThrottleOptions){const[throttled,setThrottled]=useState(value);const{ run }=useThrottleFn(()=>{setThrottled(value);},options);// 在组件挂载时就更新了一次,导致 wait ms 内不会相应更新useEffect(()=>{run();},[value]);returnthrottled;}
functionuseThrottle<T>(value: T,options?: ThrottleOptions){const[throttled,setThrottled]=useState(value);constisFirstTimeRunEffect=useRef<boolean>(true);const{ run }=useThrottleFn(()=>{setThrottled(value);},options);useEffect(()=>{if(!isFirstTimeRunEffect.current){run();}// 第一次 run Effect 时不 run 即可else{isFirstTimeRunEffect.current=false}},[value]);returnthrottled;}
如果我说的没问题,我愿意 pr。
The text was updated successfully, but these errors were encountered:
tjx666
changed the title
[useThrottle] 组件渲染后在 wait 之前更新 state
[useThrottle] 组件渲染后在 wait 之前更新 state 不会立即更新 throttledValue
Feb 20, 2024
源码:
测试代码:
解决方案:
如果我说的没问题,我愿意 pr。
The text was updated successfully, but these errors were encountered: