有没有办法在React-Query的QueryClient上配置基本URL?
好吧,我有一个非常简单的文件,可以从React-Query库中导出一个QueryClient:
/QueryClient.ts,
import { QueryClient } from "react-query";
export const queryClient = new QueryClient();
有没有办法设置基本URL,还是我应该使用诸如上下文提供商之类的东西来做到这一点?
Well, I have this very simple file where I export a QueryClient from react-query library:
/queryClient.ts
import { QueryClient } from "react-query";
export const queryClient = new QueryClient();
Is there a way to set up a base url, or should I use something like context provider in order to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,如本文所示:
“ https://tkdodo.eu/blog/reaect-query-and-ytype-script”
默认查询函数
您可以配置默认查询函数:
能够调用useQuery实例仅通过您的API方法的参数来调用:
Well, as shown in this article:
React Query and TypeScript
And on the docs:
Default Query Function
You can configure a default query function:
Being able to call the UseQuery instance passing only the params of your api method call:
ReactQuery
是包装器。提出请求的不是工具。您不能在其中定义基本URL。如果您需要配置网络请求,则需要在执行请求的工具上进行操作,而不是在包装器上进行。您可以为fetch
api或axios
软件包设置默认配置。代码>查询或
突变
,将使用此基本URL。但是,如果由于某种原因您实际上需要配置
ReactQuery
本身而无需触摸HTTP客户端,则可以定义默认的突变
或QUERY> QUERY> QUERY
正如您在上一个答案中指出的那样,请按照您想要的方式使用HTTP客户端。但这不是灵活的,因为您不仅定义了基本URL,还可以定义整个查询功能。如果在某些情况下需要其他QueryFN或MUTATIONFN,则必须再次指定基本URL。从文档( https://tanstack.com/查询/v4/docs/framework/react/guides/default-Query-function ):
reactQuery
is a wrapper. It is not the tool that makes the request. You cannot define a base url in it. If you need to configure the network request, you need to do it on the tool that does the request, not on the wrapper. You can set a default configuration for thefetch
API or for theaxios
package, for example:That way, when you use
axios
in aquery
ormutation
, this base url will be used.However if for some reason you actually need to configure
reactQuery
itself without touching the HTTP client, you can define a defaultmutation
orquery
function that will use the HTTP client the way you want, as you pointed out in the previous answer. But that is less flexible, as you don't just define a base url, but the whole query function. If you need a different queryFn or mutationFn in some case, then you have to specify the base url again.From the docs (https://tanstack.com/query/v4/docs/framework/react/guides/default-query-function):