Javascript Debounce不';t触发函数
我正在处理React组件,我希望将Javascript Debounce不';t触发函数,javascript,reactjs,lodash,Javascript,Reactjs,Lodash,我正在处理React组件,我希望将debounce绑定到输入。我的代码如下: class Form extends React.Component { constructor() { ... this.deb = this.deb.bind(this); } ... this.deb() { debounce(() => { console.log('bam'); }, 400, false); } render() { retu
debounce
绑定到输入。我的代码如下:
class Form extends React.Component {
constructor() {
...
this.deb = this.deb.bind(this);
}
...
this.deb() {
debounce(() => { console.log('bam'); }, 400, false);
}
render() {
return (
<input
onChange={this.deb}
/>
)
}
}
类表单扩展了React.Component{
构造函数(){
...
this.deb=this.deb.bind(this);
}
...
这个.deb(){
去抖动(()=>{console.log('bam');},400,false);
}
render(){
返回(
)
}
}
没有
debounce
在this.deb中,一切正常。我能做什么?debounce
会创建一个新的函数的非bounce版本,因此在这种情况下,只需在构造函数中执行一次:
class Form extends React.Component {
constructor(props) {
super(props);
this.deb = debounce(this.deb.bind(this), 400, false);
}
deb() {
console.log('bam');
}
render() {
return <input onChange={this.deb} />;
}
}
类表单扩展了React.Component{
建造师(道具){
超级(道具);
this.deb=debounce(this.deb.bind(this),400,false);
}
deb(){
console.log('bam');
}
render(){
返回;
}
}
效果很好,我知道这很简单;)非常感谢@Tholle@太棒了!没问题。:)