当我使用多个嵌套swrconfig时,选择哪些选项?

发布于 2025-02-07 01:08:05 字数 363 浏览 2 评论 0原文

我目前正在使用swrconfig拥有全局fetcher,但是我也想在某些组件中覆盖这个fetcher,是否会使用上层swrconfig选项?

<SWRConfig
    value={{
      fetcher: (resource, init) =>
        fetch(resource, init).then((res) => res.json()),
    }}
  >
       ...
         <SWRConfig
             value={{
               fetcher: customFetch
            }}
          >

I am currently using SWRConfig for having a global fetcher, but I also want to overwrite this fetcher in some components, would the upper level SWRConfig options be used?

<SWRConfig
    value={{
      fetcher: (resource, init) =>
        fetch(resource, init).then((res) => res.json()),
    }}
  >
       ...
         <SWRConfig
             value={{
               fetcher: customFetch
            }}
          >

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

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

发布评论

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

评论(1

弥繁 2025-02-14 01:08:05

将扩展嵌套swrconfig配置。对于您的方案,这意味着如果您调用在第二个swrconfig内使用,它将使用在此处定义的fetcher(custicfetch)。

另外,您还可以在上定义fetcher直接调用以在任何时候覆盖全局。

useSWR('my-key', customFetch)

Nested SWRConfig configurations will be extended. For your scenario, it means if you call useSWR inside the second SWRConfig it will use the fetcher defined there (customFetch).

Alternatively, you can also define the fetcher on the useSWR call directly to override the global one at any point.

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