Skip to content

Ability to catch errors in swr hook onError and global SWRConfig onError at the same time #2556

Answered by koba04
nik-webdevelop asked this question in Q&A
Discussion options

You must be logged in to vote

You can use the global onError by calling it from the onError in the component.

const Component = () => {
  const { onError } = useSWRConfig();
  useSWR("/api/data", (url) => fetch(url).then((r) => r.json()), {
    onError: (...args) => {
      const [error] =args;
      console.log("hook onError:", error);
      onError(...args);
    }
  });

  return null;
};

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@nik-webdevelop
Comment options

Answer selected by nik-webdevelop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2534 on April 09, 2023 13:27.