当时做查询input框输入的内容时,每次input的内容改变都会调用一次接口,这比较耗用性能,就查到了可以使用节流函数debounce。
下载debounce
第一步:引入debounce
import {debounce} from ‘lodash’;
第二步:对要进行节流的函数进行设置,小于设置的时间内,改变input的内容,不会持续发接口;
export default class App extends React.Component {
constructor(props) {
super(props);
// input框中节流设置
this.handleInputChange = debounce(this.handleInputChange, 500);
}
}
第三步:写该函数的具体逻辑。
handleInputChange= (value: string) => {
// 调接口的地方
}