javascript - 在 react 中的 onchange 事件中调用两个函数

标签 javascript reactjs onchange

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

我无法同时调用两个函数。我没有使用此示例代码添加模拟 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() }}
/>

关于javascript - 在 react 中的 onchange 事件中调用两个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54032379/

相关文章:

javascript - ReactJS + D3 : Parse local CSV file and passing it to state with d3-request

MySQL,检测排序索引上的行值更改

javascript - 在 Angular js 中链接 promise

javascript - 如何根据js中的父位置动态调整div的大小

javascript - 单击按钮时暂停 jQuery

javascript - Prop 在异常情况下丢失

reactjs - 使用 HoC 时如何正确分配通用 Prop ?

javascript - Material UI 时间选择器 UTC

javascript - HTML5 <输入类型 =“date”> - onChange 事件

jQuery-检测 SPAN 字段内容的变化