在Reaction中调用onChange事件内的两个函数

2022-05-05 00:00:00 reactjs javascript onchange

我尝试使用onChange事件在Reaction中动态调用两个函数以实现搜索功能。在第一个函数中,我设置了值的状态,而在第二个函数中,我必须调用该值并执行该函数。

我无法同时调用两个函数。我不会在此示例代码中添加模拟JSON。

 handleChange(e) {
   this.setState({ value: e.target.value.substr(0, 20) });
}
filterFunction(filteredSections) {
  let search = filteredSections;
  search = filteredSections.filter(somesection => somesection.insidesection && somesection.insidesection.name.toLowerCase().indexOf(this.state.value.toLowerCase()) !== -1);
return search;
    }
 render() {
    const filteredSections = othersection;
return(

<div>
<FormControl
    name="searching"
    placeholder="Searching"
    onChange={e => this.handleChange(e).bind(this)}
    onChange={() => this.filterFunction(filteredSections)}
/>
</div>
<div>
    {filteredSections.map(section =><othersection customization={section} } }
</div>
)
}

预期的onChange事件必须同时调用这两个函数。


解决方案

一个处理程序只能分配给onChange一次。当您使用多个这样的赋值时,第二个赋值将覆盖第一个赋值。

您要么必须创建调用这两个函数的处理程序,要么使用匿名函数:

twoCalls = e => {
  this.functionOne(e)
  this.functionTwo()
}
.
.
.
<FormControl
    name="searching"
    placeholder="Searching"
    onChange={this.twoCalls}
/>

或...

<FormControl
    name="searching"
    placeholder="Searching"
    onChange={e => { this.functionOne(e); this.functionTwo() }}
/>

相关文章