尝试在具有更改时更新的输入字段的反应组件上创建延迟
这是我的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);
我没有得到辩解。我在这里错过了什么?
答案 0 :(得分:1)
我看到你使用<string name="first_name">"الاسم الخاص"</string>
<string name="last_name">"اسم العائلة"</string>
,所以我假设this
在你的有状态组件的渲染函数中。在这种情况下,请确保在此有状态组件的FormControl
中进行绑定和去抖动。
```
constructor
```
答案 1 :(得分:0)
请阅读解释其工作原理的评论
class Input extends Component {
static propTypes = {
onChange: PropTypes.func.isRequired,
value: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
]),
}
state = {
value: '',
}
// When component receives updated `value` from outside, update internal `value` state.
componentWillReceiveProps(nextProps) {
this.setState({ value: nextProps.value });
}
// Store updated value localy.
onChange = (event) => {
this.setState({ value: event.target.value });
}
onBlur = (event) => {
// Trigger change to external env.
this.props.onChange(this.state.value);
// Also, don't forget to call `onBlur` if received from parent.
if (this.props.onBlur) {
this.props.onBlur(event);
}
}
render() {
return <input {...this.props} value={this.state.value} onChange={this.onChange} onBlur={this.onBlur} />
}
}