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
import{useState,useEffect}from'react';importuseDebounceFnfrom'../useDebounceFn';import{DebounceOptions}from'./debounceOptions';functionuseDebounce<T>(value: T,options?: DebounceOptions){// value 和 debounced是不同步的const[debounced,setDebounced]=useState(value);const{ run }=useDebounceFn(()=>{setDebounced(value);},options);// 观测value的变化,然后调用防抖版的set函数useEffect(()=>{run();},[value]);returndebounced;}exportdefaultuseDebounce;
import{useState,useEffect}from'react';importuseThrottleFnfrom'../useThrottleFn';import{ThrottleOptions}from'./throttleOptions';functionuseThrottle<T>(value: T,options?: ThrottleOptions){const[throttled,setThrottled]=useState(value);const{ run }=useThrottleFn(()=>{setThrottled(value);},options);useEffect(()=>{run();},[value]);returnthrottled;}exportdefaultuseThrottle;
因为无论节流还是防抖,都是基于定时器实现的,所以统一放在这里。防抖和节流非常相似,功能上分别对函数/值/effect 进行处理,实现上也都是基于lodash提供的工具函数。
useInterval
useInterval相隔delay时间执行fn
useTimeout
有useInterval就有useTimeout。因为回调只需要被执行一次,所以没有immediate选项。
useCountDown
处理倒计时的定时器
useDebounceFn
underscore、lodash都提供了debounce方法防抖动,这是hook版本的防抖。把函数转换成防抖版本。
useDebounce
useDebounceFn是处理函数的防抖,useDebounce是处理值的防抖。
useDebounceEffect
useEffect的debounce版
useThrottleFn
useThrottleFn是对函数节流的hook,从功能上和实现上都非常类似useDebounceFn。
useThrottle
useThrottle是对值节流的hook,从功能和实现上非常接近useDebounce.
useThrottleEffect
对useEffect进行节流的hook
The text was updated successfully, but these errors were encountered: