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
-
Is there a way to catch errors on swr hook error handler and global SWRConfig error handler at the same time? Currently, if I set the onError in the hook's config, the global onError is not called. SWR version. |
Beta Was this translation helpful? Give feedback.
Answered by
koba04
Apr 9, 2023
Replies: 1 comment 1 reply
-
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;
}; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
nik-webdevelop
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use the global onError by calling it from the onError in the component.