Returning undefined from fetcher function causes suspense to loop infinitely. #2309
Unanswered
kiranmarshall
asked this question in
Ideas
Replies: 1 comment
-
This is the behavior by design, because we use I’d recommend returning |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Bug report
Description / Observed Behavior
I would like to cancel the fetcher function before it is executed, and return undefined to
useSwr
so that I can use a fallback return type (empty array or object, for example) for the UI.The return type of the fetcher is
Promise<ExpectedReturn[] | undefined>
. Annoyingly this pattern has worked in all cases up until now, on one specific API call. Changingundefined
tofalse
and early returningfalse
from the fetcher function produces the expected behaviour.What kind of issues did you encounter with SWR?
Receiving undefined from the fetcher caused Swr to loop infinitely, but never threw an error or warning of any kind.
Expected Behavior
Receiving undefined from the fetcher function should cause
const { data } = useSwr...
to evaluate to undefined, and the fallback ofreturn { data: data || [] }
should be returned.Repro Steps / Code Example
This is production code, so it is heavily redacted. Annoyingly this is happening in one specific case making it hard for me to diagnose.
Or share your code snippet or a CodeSandbox link is also appreciated!
Additional Context
As highlighted in the code block, we are passing a token to the axios fetcher if it does exist. This has not cause issues elsewhere but may be of significance.
The
useSwrWrapper
is not being called illegally/break any rules of hooks from the function component.Dependencies/Dev dependencies that may be relevant:
SWR version.
Add any other context about the problem here.
Please feel free to let me know if this is in the wrong place. First public issue I have posted.
Beta Was this translation helpful? Give feedback.
All reactions