Redux-Saga vs rtk-Query,还有什么更好的选择?

发布于 2025-01-23 13:34:48 字数 1394 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕花眠 2025-01-30 13:34:48

他们甚至没有做同样的事情。

RTK查询是一种为您管理API缓存逻辑的单点工具。

Redux Saga是一种编排工具,可让您几乎做任何事情 - 但是您必须自己写。这可能意味着要编写很多代码。

因此,基本问题是“我需要一些东西来触发API”或“我想自己写代码来做某事”。 之后,您得到的问题是Redux Saga根本是一个好主意 - 大多数时候,不是。 Redux Saga是一种具有很多功能的动力工具 - 还增加了很多复杂性。 redux样式指南的建议是每项工作的最简单工具:使用redux thunk作为异步逻辑。如果您是需要“反应性逻辑”的10%应用程序之一,请使用RTK (仅适用于这些东西,请与简单的东西呆在一起!)。只有当您属于那两个更简单的工具实际上不可能使用的应用程序的1%的应用程序时,请使用SAGA。即使那样,它也将其与thunks混合。

They are not even doing the same thing.

RTK Query is a single-purpose tool to manage api caching logic for you.

Redux Saga is an orchestration tool that allows you to do pretty much anything - but you have to write it yourself. And that might mean a lot of code to write.

So the base question is "do I need something to trigger an api" or "do I want to write code myself to do something". After that you get the question is Redux Saga is a good idea at all - and most of the time, it isn't. Redux Saga is a power tool with a lot of functionality - that also adds a lot of complexity. The recommendation from the Redux Style Guide is to use the most simple tool for every job: Use Redux Thunk for async logic. If you are one of the 10% apps that needs "reactive logic", use the RTK listener middleware (only for those things, stay with thunks for the simple stuff!). Only if you belong to those 1% of apps that needs something that is actually not possible with those two much more simple tools, use saga. And even then, mix it with thunks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文