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

Passing an array of keys doesn't cache the keys individually #3018

Open
ricky-orchid opened this issue Sep 11, 2024 · 1 comment
Open

Passing an array of keys doesn't cache the keys individually #3018

ricky-orchid opened this issue Sep 11, 2024 · 1 comment

Comments

@ricky-orchid
Copy link

ricky-orchid commented Sep 11, 2024

Bug report

Description / Observed Behavior

Passing an array to useSWR([key, key], fetcherFn) doesn't seem to cache the keys individually.

Expected Behavior

I should be able to call the keys individually and expect a cached result

Repro Steps / Code Example

function fetchUsers(...keys) {
  return Promise.all(
    keys.map((o) => fetchUserUtil(o.split('/')[2]))
  );
}
const { data } = useSWR(
  ids.map((v) => `/users/${id}`),
  fetchUsers,
  { dedupingInterval: 1000 * 60 }
);

// Then later when i call the following, a new request is made...

const { data } = useSWR(
  `/users/1`,
  fetchUsers,
  { dedupingInterval: 1000 * 60 }
);

Additional Context

SWR version: 1.3.0

@ricky-orchid ricky-orchid changed the title Passing an array of keys doesn't get cached individually Passing an array of keys doesn't cache the keys individually Sep 11, 2024
@koba04
Copy link
Collaborator

koba04 commented Sep 23, 2024

@ricky-orchid
That's an expected behavior, SWR treats an array key as a single key, not each element as an individual key.
https://swr.vercel.app/docs/arguments

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