我有下面的反应组件。 调用raiseCriteriaChange方法但从未到达this.props.onCriteriaChange(this.state.criteria)行。
为什么代码永远不会达到 this.props.onCriteriaChange 的任何想法?
import * as React from 'react';
import { debounce } from 'lodash';
interface CustomerSearchCriteriaProps {
onCriteriaChange: (criteria: string) => void;
}
interface CustomerSearchCriteriaState {
criteria: string;
}
class CustomerSearchCriteria extends React.Component<CustomerSearchCriteriaProps, CustomerSearchCriteriaState> {
constructor() {
super();
this.state = {
criteria: ''
};
}
// problem method:
raiseCriteriaChange = () => {
// the call reaches here fine ...
debounce(
() => {
// ... but the call never reaches here ... why is this?
this.props.onCriteriaChange(this.state.criteria);
},
300);
}
handleCriteriaChange = (e: React.FormEvent<HTMLInputElement>) => {
this.setState({ criteria: e.currentTarget.value }, () => {
this.raiseCriteriaChange();
});
}
render() {
return (
<div className="input-group">
<input
type="text"
value={this.state.criteria}
onChange={this.handleCriteriaChange}
className="form-control"
/>
</div>
);
}
}
export default CustomerSearchCriteria;
答案 0 :(得分:4)
_.debounce
只返回一个必须调用的函数。尝试更改您的代码,如:
raiseCriteriaChange = debounce(() => {
this.props.onCriteriaChange(this.state.criteria);
}, 300);
答案 1 :(得分:0)
请参阅_.debounce,其中说明了
(功能):返回新的去抖动功能。
所以你需要像
一样调用它var debounced = debounce(
() => {
// ... but the call never reaches here ... why is this?
this.props.onCriteriaChange(this.state.criteria);
},
300
);
debounced();