[英]Add debounce in React.js
此功能在onChange
操作上执行。 我只想评论下的内容在1秒后反跳。 我该怎么做?
onChange = (event, { newValue }) => {
this.setState({
value: newValue,
}, () => {
if (this.state.value.length !== 26) {
// this debounce \/
this.Auth.getUsersData(newValue).then(res => {
if (res) {
this.setState({
accountBills: res,
});
}
});
};
});
}
只需将代码提取到新方法中,然后将其包装在_.debounce
中_.debounce
:
import {debounce} from 'lodash';
getUserData = debounce(newValue => this.Auth.getUsersData(newValue)
.then(res => {
if (res) { this.setState({ accountBills: res }) }
})
, 1000);
onChange = (event, { newValue }) => {
this.setState({
value: newValue,
}, () => {
if (this.state.value.length !== 26) {
this.getUserData(newValue);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.