English 中文(简体)
在按钮中点击时会调用自动同步函数
原标题:Can async functions be called in onClick in button react
  • 时间:2019-02-20 05:20:15
  •  标签:
  • reactjs
The bounty expires in 7 days. Answers to this question are eligible for a +50 reputation bounty. Alexis Tyler is looking for an answer from a reputable source.

我要在按钮中的 点击 事件中调用 Async 函数。 但是它不起作用。 是否可以在点击按钮中调用 Async 函数?

这是密码

< 加固> Filter 按钮

const FilterButton = (props) => {

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

抓取发票同步函数

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

将函数传送到组件的代码

 <SearchField store = {this.props.store} fetchInvoices={this.fetchInvoices}/>
最佳回答

这就是我如何在 onClick 中称我的音符:

<Button onClick={async () => {await this.asyncFunc("Example");} }/>

async asyncFunc(text) {
    console.log(text);
}
问题回答

您的 < a href= > "https://javascript.info/async-await" rel= "noreferrer" >async 函数 :

const asyncFunc = async () => {
    let promise = new Promise((resolve, reject) => {
        setTimeout(() => resolve("I am a done promise!"), 3000)
    });

    let result = await promise

    alert(result);
}

您的 < a href=> "https://reactjs.org/docs/rapping-events.html" rel=" noreferrer">按钮组件 呼叫:

<button onClick={asyncFunc} >I am a button!</button>




相关问题
How to use one react app into another react app?

I have two react apps, parent app and child app. and child app have an hash router. I have build the child app using npm run build, It creates build folder. That build folder moved into inside the ...

how to get selected value in Ant Design

I want to print the selected value, and below is my option list: const vesselName = [ { value: 0 , label: ALBIDDA , }, { value: 1 , label: ALRUMEILA , }, { value: 2 ,...

How to add a <br> tag in reactjs between two strings?

I am using react. I want to add a line break <br> between strings No results and Please try another search term. . I have tried No results.<br>Please try another search term. but ...

热门标签