没有在反应组件中调用去抖功能
debounced function not being called in a react component
我有下面的反应组件。
调用了 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;
_.debounce
只是 returns 一个必须调用的函数。尝试更改您的代码:
raiseCriteriaChange = debounce(() => {
this.props.onCriteriaChange(this.state.criteria);
}, 300);
参见 _.debounce,上面写着
(Function): Returns the new debounced function.
所以你需要这样称呼它
var debounced = debounce(
() => {
// ... but the call never reaches here ... why is this?
this.props.onCriteriaChange(this.state.criteria);
},
300
);
debounced();
我有下面的反应组件。 调用了 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;
_.debounce
只是 returns 一个必须调用的函数。尝试更改您的代码:
raiseCriteriaChange = debounce(() => {
this.props.onCriteriaChange(this.state.criteria);
}, 300);
参见 _.debounce,上面写着
(Function): Returns the new debounced function.
所以你需要这样称呼它
var debounced = debounce(
() => {
// ... but the call never reaches here ... why is this?
this.props.onCriteriaChange(this.state.criteria);
},
300
);
debounced();