【发布时间】:2017-06-16 04:48:28
【问题描述】:
尝试在具有随更改更新的输入字段的反应组件上创建延迟
这是我的 onChange 方法
handleOrderQtyKeyPress (e) {
var regex = /[^0-9]/
if (e.key.match(regex)) {
e.preventDefault();
}
if (this.state.orderQtyValue.toString().length == 3) {
e.preventDefault();
}
}
和react-bootstrap 组件:
<FormControl
type='number'
min='0'
value={this.state.orderQtyValue}
onChange={this.handleOrderQtyChange}
onKeyPress={this.handleOrderQtyKeyPress}
style={styles.orderQtyValue}
/>
所以我尝试导入 lodash _.debounce 并在构造函数中应用
import debounce from 'lodash/debounce';
this.handleOrderQtyKeyPress = _.debounce(this.handleOrderQtyKeyPress.bind(this),1000);
我没有得到去抖动。我在这里想念什么?
【问题讨论】:
-
你想达到什么目的?你为什么要对更改事件进行去抖动?
-
去抖以防止每次在输入字段中输入数字时触发 onChange。
标签: javascript reactjs ecmascript-6 react-bootstrap debounce