可以在没有QueryArgs的情况下定义RTK Query查询吗?

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

Is it possible to define RTK Query queries without QueryArgs?

问题

在RTK Query中,对于一个查询的定义是 query<ResultType, QueryArg>。对于那些不需要向API发送任何内容的端点(例如getAllX),你可以在模板中传递什么作为QueryArg呢?它不接受只有一个参数的情况。

到目前为止,我使用了诸如 undefinednull 这样的类型来表示不需要发送任何内容,但当你使用钩子时看起来不太美观:const {...} = useGetXQuery(undefined),我相信一定有更好的方法,但是在互联网上搜索没有找到相关结果。

英文:

The definition for a query in RTK Query is query&lt;ResultType, QueryArg&gt;. All of this is fine for queries where I actually need to send parameters to the API. However, I have some endpoints which don't require anything to be sent (something like getAllX). What can I pass as QueryArg here in the template? It won't accept having one argument.

So far I've used types like undefined and null to denote that nothing needs to be sent, but it looks ugly when you use the hook: const {...} = useGetXQuery(undefined), and I'm pretty sure there has to be a better way, but scouring the internet yielded no results.

答案1

得分: 6

已解决,您需要将void用作类型,然后可以像这样使用该钩子:const {...} = useGetXQuery()

英文:

Figured it out, you need to use void as a type, then you can use the hook like this: const {...} = useGetXQuery().

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

发表评论

匿名网友

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

确定