介绍
主要介绍 setInterval(管理 setInterval)、setInterval(管理 setInterval)、useDebounce(对值的防抖)、useDebounceFn(对函数的防抖)、useThrottle(对值的节流)、useThrottleFn(对函数的节流)
在线演示:Domesy/SideEffect
管理 setInterval 的函数
- useInterval:简单的理解就是,每隔一段时间就执行的函数,类似于轮询
- 使用:三个参数 fn delay option
- fn:重复调用的函数
- delay:间隔时间,当值为 null 或 undefined 会停止计时器
option:对象,包含 immediate(是否在首次进行渲染,默认为false)
代码演示
详细代码
```typescript import React, { useState } from ‘react’; import { Button } from ‘antd’; import { useInterval } from ‘ahooks’;
const Test: React.FC
= () => { const [count, setCount] = useState(0); useInterval(() => {
setCount(count + 1);
}, 1000);
return (
<>
<div style={{fontWeight: 'bolder'}}>基础用法:</div>
<div style={{marginTop: 8}}>每隔1000ms,数字加 1: {count}</div>
</>
); }
const Mock: React.FC
= () => { const [count, setCount] = useState(0); const [interval, setInterval] = useState (1000); useInterval(
() => {
setCount(count + 1);
},
interval,
{ immediate: true },
);
return (
<>
<Test />
<div style={{fontWeight: 'bolder'}}>高阶用法:</div>
<div style={{marginTop: 8}}>每隔{interval || 0}ms,数字加 1: {count}</div>
<div style={{marginTop: 8}}>相隔时间:{interval}</div>
<div style={{marginTop: 8, display: 'flex', justifyContent: 'flex-start'}}>
<Button type='primary' style={{marginRight: 16}} onClick={() => {
if(typeof interval === 'number') setInterval(interval + 1000)
}}>加1s</Button>
<Button type='primary' style={{marginRight: 16}} onClick={() => setInterval(1000)}>重置</Button>
<Button type='primary' style={{marginRight: 16}} onClick={() => setInterval(null)}>清除</Button>
</div>
</>
);
};
export default Mock
<a name="DlBGq"></a>
## 管理 setTimeout 的函数
- **useTimeout:处理计时器函数的Hook**
- **使用:两个参数 fn delay**
- **fn:重复调用的函数**
- **delay:间隔时间,当值为 null 或 undefined 会停止计时器**
<a name="j2TKe"></a>
### 代码演示
![image.png](https://cdn.nlark.com/yuque/0/2021/png/310196/1636105328770-746451ae-8177-465d-876c-cfadac5a2bde.png#clientId=u0c3b53d6-fcad-4&from=paste&height=122&id=u0dabdf01&margin=%5Bobject%20Object%5D&name=image.png&originHeight=122&originWidth=786&originalType=binary&ratio=1&size=7558&status=done&style=none&taskId=ua9345eb9-cdb5-4a2d-9127-7495a6b8acf&width=786)
<a name="wKTJq"></a>
### 详细代码
```typescript
import React, { useState } from 'react';
import { Button } from 'antd';
import { useTimeout } from 'ahooks';
const Mock: React.FC<any> = () => {
const [count, setCount] = useState(0);
useTimeout(() => {
setCount(count + 1);
}, 2000);
return (
<div>2000ms 后数字加1: {count}</div>
);
};
export default Mock;
对值的防抖
- useDebounce:对值进行防抖的hook
- 使用:const debouncedValue = useDebounce(value: any, { wait: number}])
-
代码演示
详细代码
import React, { useState } from 'react';
import { Button } from 'antd';
import { useDebounce } from 'ahooks';
const Mock: React.FC<any> = () => {
const [value, setValue] = useState<string>('');
const debouncedValue = useDebounce(value, { wait: 500 });
return (
<div>
<input
value={value}
onChange={(e) => setValue(e.target.value)}
placeholder="Typed value"
style={{ width: 280 }}
/>
<p style={{ marginTop: 16 }}>500ms后才会变化: {debouncedValue}</p>
</div>
);
};
export default Mock;
对函数的防抖
useDebounceFn:对函数进行防抖的hook
- 频繁调用run方法,但只会执行最后一遍
- 使用方法: const { run, cancel, flush} = useDebounceFn(fn: (…args:any) => any, { wait: number})
run(防抖进行的函数)、cancel(取消当前防抖)、flush(当前防抖立即调用)
代码演示
详细代码
import React, { useState } from 'react';
import { Button } from 'antd';
import { useDebounceFn } from 'ahooks';
const Mock: React.FC<any> = () => {
const [value, setValue] = useState(0);
const { run } = useDebounceFn(
() => {
setValue(value + 1);
},
{
wait: 500,
},
);
return (
<div>
<p style={{ marginTop: 16 }}> 有效点击次数: {value} </p>
<Button type="primary" onClick={() => {run()}}>快速点击</Button>
</div>
);
};
export default Mock;
对值的节流
useThrottle:对值进行节流的hook
- 频繁调用run方法,但只会执行最后一遍
- 使用方法: const throttledValue = useThrottle(value: any, { wait: number})
代码演示
详细代码
import React, { useState } from 'react';
import { Button } from 'antd';
import { useThrottle } from 'ahooks';
const Mock: React.FC<any> = () => {
const [value, setValue] = useState<string>();
const throttledValue = useThrottle(value, { wait: 500 });
return (
<div>
<input
value={value}
onChange={(e) => setValue(e.target.value)}
placeholder="输入值"
style={{ width: 280 }}
/>
<p style={{ marginTop: 16 }}>每隔 500ms 变化一次: {throttledValue}</p>
</div>
);
};
export default Mock;
对函数的节流
- useThrottleFn:对函数进行节流的hook
- 频繁调用 run,但只会每隔 500ms 执行一次相关函数。
- 使用方法: const { run, cancel, flush} = useThrottleFn(fn: (…args:any) => any, { wait: number})
run(节流进行的函数)、cancel(取消当前节流)、flush(当前节流立即调用)
代码演示
详细代码
import React, { useState } from 'react';
import { Button } from 'antd';
import { useThrottleFn } from 'ahooks';
const Mock: React.FC<any> = () => {
const [value, setValue] = useState(0);
const { run } = useThrottleFn(
() => {
setValue(value + 1);
},
{ wait: 500 },
);
return (
<div>
<p style={{ marginBottom: 16 }}> Clicked count: {value} </p>
<Button type='primary' onClick={run}>快速点击</Button>
</div>
);
};
export default Mock;