相关疑难解决方法(0)

可以在按钮反应的 onClick 中调用异步函数吗

我将onclick在按钮中的事件中调用异步函数。但它不起作用。是否可以在按钮 onlick 中调用异步函数?

这是代码。

过滤按钮

const FilterButton = (props) => {

return (
    <div className="p-2 ">
        <button className="btn btn-secondary" onClick={props.fetchInvoices}>Filter</button>
    </div>
 );
};
Run Code Online (Sandbox Code Playgroud)

fetchInvoices 异步函数

fetchInvoices = async () => {
    const invoices = await getInvoices(this.currentState.params);
    this.props.store.dispatch(UpdateInvoices(invoices));
};
Run Code Online (Sandbox Code Playgroud)

将函数传递给组件的代码

 <SearchField store = {this.props.store} fetchInvoices={this.fetchInvoices}/>
Run Code Online (Sandbox Code Playgroud)

reactjs

21
推荐指数
2
解决办法
3万
查看次数

标签 统计

reactjs ×1