React.js - 如何在完全执行另一个方法之后调用方法

时间:2018-05-20 03:47:38

标签: javascript node.js reactjs redux react-redux

我有两个简单的功能。我试图在我的" handleSearchClick"中调用两个函数。单击按钮时功能。

我想运行" this.props.getNewsDesc()"在" this.props.fetchNewsApiOrg(this.newsSourceName)"已完全执行。

以下是代码:



handleSearchClick = () => {
    this.props.fetchNewsApiOrg(this.newsSourceName);
    
    this.props.getNewsDesc();
  }




你可以告诉我在另一个功能完全执行后调用函数的正确方法吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以将该方法作为参数传递,并在fetchNewsApiOrg()中调用该函数。

handleSearchClick = () => {
    this.props.fetchNewsApiOrg(this.newsSourceName, this.props.getNewsDesc);
 }
...
fetchNewsApiOrg(newsSourceName,getNewDesc){
...
getNewDesc();
}

在这个方法中,你必须绑定getNewDesc, 构造函数中的this.getNewDesc = this.getNewDesc.bind(this)

或者你可以接受承诺。

handleSearchClick = () => {
    this.props.fetchNewsApiOrg(this.newsSourceName).then(()=>{
    this.props.getNewsDesc();
  })

此处fetchNewsApiOrg应返回promise