useDebounceFn
使用React hooks防抖(debounce)函数
Usage
实时编辑器
function Demo() { const [value, setValue] = useState(0); const { run } = useDebounceFn(() => { setValue(value + 1); }, 500); return ( <div> <p style={{ marginTop: 16 }}> 点击次数: {value} </p> <button type="button" onClick={run}> 快速点击! </button> </div> ); };
结果
点击次数: 0
API
useDebounceFn
Returns
{ run: _.DebouncedFunc<(...args_0: Parameters<T>) => ReturnType<T>>; cancel: () => void; flush: any; }: 具有以下元素的对象:
- run:执行函数。
- cancel:取消执行函数。
- flush: 立即执行函数
Arguments
| 参数名 | 描述 | 类型 | 默认值 |
|---|---|---|---|
| fn | 要防抖的函数 | T (必填) | - |
| wait | 间隔时间 | number | undefined | - |
| options | 传递给 lodash.debounce 的属性 | _.DebounceSettings | undefined | - |