如何使用React Redux JavaScript按顺序进行多个API终点调用?
我有一个Redux Store,其中包含多个状态,该状态由同一API但多个终点填充。
我有一个带有多个组件的React应用程序,可以访问这些状态之一,并且它们都通过将异步功能(Redux-Thunk)派遣到商店来呼吁通讯API no安装。
在app.js中使用多个组件。
问题: 当所有组件安装时,他们同时致电API并超过短时间范围允许的最大呼叫数量。因此,所有呼叫都用状态代码429回复:请求太多。
*额外的注释:我正在销售新闻网络应用,并使用带有多个搜索查询和终点的gnews.io API获取数据。
I have a redux store containing multiple states which is populated by same api but multiple end-points.
I have a react app with multiple components, which access to one of these states, and they all call to the correspondent api no mount by dispatching a async function (redux-thunk) to the store.
In the App.js multiple components are used.
Problem:
When all the components mount they call to the api concurrently and exceeds the maximum number of call allowed for the short time frame. Thus all calls reply with status code 429: Too many request.
*Extra notes: I'm buliding a news web-app, and using gnews.io api with multiple search queries and end-points to get data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的情况下,我相信一种方法是创建一个请求类型并在您的状态中使用它,而不是从端点返回的原始值,例如:
当您派遣将触发端点的操作时,您会设置API状态到加载。
之后,您需要做的就是检查请求是否尚未在其他组件上运行。
从理论上讲,您可以将所有初始请求分配到父组件上,并且只有在完成初始请求后才能渲染这些子组件。但这取决于项目基础IMO的项目。
In your case i believe one approach would be to create a request type and use it in your state instead of the raw value returned from the endpoint, something like:
When you dispatch an action that will trigger the endpoint in thunks you would set the api state to loading.
After that all you need to do is check if the request is not already running on the other components.
In theory you could dispatch all of the initial requests on a parent component and only render these child components when the initial requests are done. But this depends on a project to project basis IMO.