【发布时间】:2021-10-05 18:18:23
【问题描述】:
我将在按钮的onclick 事件中调用异步函数。但它不起作用。是否可以在按钮 onlick 中调用异步函数?
这里是代码。
过滤按钮
const FilterButton = (props) => {
return (
<div className="p-2 ">
<button className="btn btn-secondary" onClick={props.fetchInvoices}>Filter</button>
</div>
);
};
fetchInvoices 异步函数
fetchInvoices = async () => {
const invoices = await getInvoices(this.currentState.params);
this.props.store.dispatch(UpdateInvoices(invoices));
};
将函数传递给组件的代码
<SearchField store = {this.props.store} fetchInvoices={this.fetchInvoices}/>
【问题讨论】:
-
它会起作用的。什么错误?
-
现在它正在工作。 SeachField 中的 fetchInvoice 作为参数传递。我添加了 fetchInvoices={this.fetchInvoices} 而不是 fetchInvoices={this.props.fetchInvoices}
标签: reactjs