gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:39:20 24 4
gpt4 key购买 nike

我正在尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com