useSWR doesn't return the response as {data,error} when an error occurs

36 Views Asked by At

I have a simple use case where I'm fetching data from an api based on the value of the router.query in NEXT JS.

const { data, error } = useSWR(() => {
  if (router.query.sort === "shortest")
    return `http://localhost:1337/api/playlists/${options?.value}?sortBy=duration`;

  if (router.query.sort === "longest")
    return `http://localhost:1337/api/playlists/${options?.value}?sortBy=duration&order=desc`;

  if (router.query.sort === "oldest")
    return `http://localhost:1337/api/playlists/${options?.value}?sortBy=id`;

  return `http://localhost:1337/api/playlists/${options?.value}?sortBy=id&order=desc`;
});

useswr doesn't return the response in the same format as the docs

When an error occurs in any one of the api endpoints, instead of reading directly the error as listed above, I found that it's returned undefined but instead another {data,error} is nested inside the data that is first returned which is different from what the docs describes. Hopefully my description is clear enough. Please clarify if I'm doing anything wrong here

0

There are 0 best solutions below