突变使用WRR数据(本地状态)而不重新验证?

发布于 2025-02-10 10:37:04 字数 371 浏览 3 评论 0原文

我有一些我使用useswr futce的数据,现在我想对此进行操作。以前,我会在API的响应中setState,然后在其上操作时,我只需用setState覆盖它。

就我而言,我想用按钮按按钮对数据进行排序。看起来像这样,

function Component() {
   const { data } = useSWR(...)

  function sortData() {
    data.sort(...)
  }
}

如何修改data,而无需使用wrr重新验证并获取新数据?严格来说,这是客户端的需求,API不必担心此分类。

谢谢!

I have some data from a public API that I fetched using useSWR, now I want to operate on that. Previously, I would setState with the response from an API and then when operating on it I would just overwrite it with setState.

In my case, I want to sort the data with a button press. it looks like this

function Component() {
   const { data } = useSWR(...)

  function sortData() {
    data.sort(...)
  }
}

How can I modify data like this without having useSWR revalidate and fetch new data? This is strictly a client-side need, the API does not need to worry about this sorting.

Thanks!

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

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

发布评论

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

评论(2

踏雪无痕 2025-02-17 10:37:05

使用 swr v2.0.4 ,实现此目的的签名是突变(键,回调,选项)

请参阅文档: https://swr.vercel.app/ docs/stumation.en-us#参数

  1. 回调:类似于React的usestate hook> hook setState setState callback IE (prevdata) => newdata
  2. 选项options.revalidate to false跳过重新验证或获取数据(在下面的示例中)。
import { mutate } from 'swr';

function Component() {
   const { data } = useSWR('key', ...)

  function sortData() {
    mutate('key',
      (prevData) => {
        const sortedData = [...prevData].sort();
        return sortedData
      },
      { revalidate: false } // prevents revalidating the cache
    )

  }
}

我相信您也可以直接从使用使用返回(但我不确定API)。大概

  const { data, mutate } = useSWR(...)
  mutate(
    (prevData) => {
      const sortedData = [...prevData].sort();
      return sortedData
    },
    { revalidate: false } // prevents revalidating the cache
  )

Using swr v2.0.4, the signature to achieve this is mutate(key, callback, options)

See the docs: https://swr.vercel.app/docs/mutation.en-US#parameters

  1. callback: similar to react's useState hook setState callback i.e (prevData) => newData
  2. options: options.revalidate to false skips revalidating or fetching data (in the example below).
import { mutate } from 'swr';

function Component() {
   const { data } = useSWR('key', ...)

  function sortData() {
    mutate('key',
      (prevData) => {
        const sortedData = [...prevData].sort();
        return sortedData
      },
      { revalidate: false } // prevents revalidating the cache
    )

  }
}

I believe you can also use mutate directly from useSWR return (but I am unsure of the API). Probably

  const { data, mutate } = useSWR(...)
  mutate(
    (prevData) => {
      const sortedData = [...prevData].sort();
      return sortedData
    },
    { revalidate: false } // prevents revalidating the cache
  )
落花随流水 2025-02-17 10:37:05

这很难说,因为您不是说是什么原因导致SWR重新验证,而是我知道的主要解决方案:

It's hard to say because you're not saying what causes SWR to revalidate but here are the main solutions I know of :

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