Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revalidate useSWR by considering only the first argument when using multiple arguments. #2854

Open
justAnArthur opened this issue Dec 11, 2023 · 1 comment

Comments

@justAnArthur
Copy link

Bug report

Description / Observed Behavior

I'm incorporating the table state, such as filters and sorting, into the body of a fetch request. Previously, I utilized a custom use hook that invoked an API call whenever path or body changed.

Upon transitioning to useSWR, I now need to pass the body as the second argument to ensure unique data retrieval for different body configurations:

useSWR([subPath, body && JSON.stringify(body)],  ...)

However, as highlighted in 363, the revalidation process requires passing the entire array of multiple arguments: mutate(["/api/users", 1]);, which seems somewhat redundant.

Expected Behavior

Mutate data by first key - url/subPath. As: mutate("/api/users");

Repro Steps / Code Example

-

Additional Context

I believe there's no necessity for a full revalidation across all arguments, as most users, like myself, primarily utilize multiple arguments solely for passing the body.

@teooliver
Copy link

I am not sure if this helps, but you can revalidate based on just one item of the array (or part of a string) like this:

https://swr.vercel.app/docs/mutation#mutate-multiple-items

From the docs:

useSWR(['item', 123], ...)
useSWR(['item', 124], ...)
useSWR(['item', 125], ...)
 
mutate(
  key => Array.isArray(key) && key[0] === 'item',
  undefined,
  { revalidate: false }
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants