Next.js Discord

Discord Forum

Should useSWRInfinite size and key index be in sync?

Unanswered
toMMYY posted this in #help-forum
Open in Discord
Hi, I'd like to sanity check this behaviour with useSWRInfinite.
I have a scenario where the size variable and the index value from the getKey callback are not matching and using an Infinite hook in a 2nd location in my app. I'm not sure if its something I've done wrong or it's legit unexpected behraviour.

Thanks!

3 Replies

So for some reason, when my cacheKey was returned as a string it doesn't work quite as expected, but as an array it works fine 🤔
@defcron sorry for the ping, but is this expected behaviour to your knowledge? I'm just wondering why returning as an array is different from returning the string on its own. e.g.
// works
useSWRInfinite(
    (pageIndex) => [
      cacheKey({
        ...queryParams,
        page: pageIndex + 1,
      }),
    ],
    async ([cacheKey]) => {
      return searcher(userId, getKey(cacheKey).params);
    }
  );

// does not
useSWRInfinite(
    (pageIndex) =>
      cacheKey({
        ...queryParams,
        page: pageIndex + 1,
      }),
// only difference is non-arrayed key
    async (cacheKey) => {
      return searcher(userId, getKey(cacheKey).params);
    }
  );