简体   繁体   English

react redux-thunk中的递归api调用的正确模式

[英]Correct pattern for recursive api call in react redux-thunk

We want to make a recursive API call based on the number of records received in the response. 我们希望基于响应中收到的记录数进行递归API调用。 For example, if the number of records we get is 10 and the total records are 20, we need to call to the same API again with an updated marker (ie, we are making calls to next 10 records). 例如,如果我们得到的记录数为10,而总记录为20,则我们需要使用更新的标记再次调用同一API(即,我们正在对下10条记录进行调用)。

So, what should be the correct pattern to call the API again? 那么,再次调用API的正确模式应该是什么? Should I do a .then() to the first dispatch(it is inside componentDidMount method) or should I call the API again instead of dispatching the first response. 我应该对第一个调度执行.then() (它在componentDidMount方法内部),还是应该再次调用API而不是调度第一个响应。

Though the first option works but it would cause the render method to be called multiple times and I am not sure if that is the correct pattern or not. 尽管第一个选项有效,但它将导致render方法被多次调用,但我不确定这是否是正确的模式。

componentDidMount() {
    store.dispatch(getCustomerData('1234',1,10)).then((res) => {
     //do some login and call store.dispatch(getCustomerData('1234',2,10)) again
   })
  }

OK, so based on on your comments I would suggest to just call one function in your componentDidMount and call your api inside this function. 好的,因此根据您的评论,我建议仅在componentDidMount中调用一个函数,然后在此函数内调用api。 After getting response you can check if there is more data and call your api again for the other datas. 得到响应后,您可以检查是否还有更多数据,然后再次调用您的api获取其他数据。 Like you said using .then() will do the work. 就像您说的那样,使用.then()完成工作。 I hope I explained myself clearly, if not please say it so I can describe it with more detail. 我希望我能清楚地解释自己,如果不能的话,请说清楚,以便更详细地描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM