处理 React web 应用中的 PUT 请求

huangapple go评论66阅读模式
英文:

Handling put requests in a React web application

问题

我正在创建一个使用React的Web应用程序,用户可以跟踪可收藏物品。React应用程序将通过API Gateway连接到AWS,并发出请求,这些请求将由Lambda处理,并将数据存储在DynamoDB表中。我还在使用React Query来缓存数据,以减少Lambda触发次数以降低成本。

每个物品都以卡片形式呈现,卡片上有一个“-”和一个“+”按钮,用户可以点击这些按钮来增加其收藏夹/数据库中相应物品的数量。处理这些请求的最基本方法是在点击任何一个按钮时都发出一个put请求,并相应地更新数据库。但这对我来说听起来非常低效。

我的问题是,如何处理put请求以最小化API调用/Lambda触发的次数?

英文:

I'm creating a web application with React where a user can keep track of collectable items. The React app will be connected to AWS via API Gateway, and will make requests which will be handled by Lambda's and the data store in a DynamoDB table. I am also making use of React Query to cache the data and minimise too many Lambda triggers to keep costs low.

Each item is rendered on a card, and the card will have a "-" and a "+" button, which the user can click to increase the quantity of said item in their collection/in the database. The most basic way to do this would be to make a put request every time either of the buttons are clicked, and updating the database accordingly. But this sounds extremely inefficient to me.

My question is, what is the best way to handle the put requests while minimising the number of API calls/Lambda triggers?

答案1

得分: 1

你可以防抖或节流你的请求,以减少API调用次数,这样可以定期进行PUT请求,而不是在每次按钮点击时进行。

这里有一个详细的指南,教你如何实现防抖和节流:

https://blog.webdevsimplified.com/2022-03/debounce-vs-throttle/

英文:

You can debounce or throttle your requests to minimize the number of API calls so that a put request is made at regular intervals rather than on every button click.

Here’s a thorough guide for how to implement denouncing and throttling:

https://blog.webdevsimplified.com/2022-03/debounce-vs-throttle/

huangapple
  • 本文由 发表于 2023年5月22日 04:53:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定