question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error cleared on next render

See original GitHub issue

Bug report

Description / Observed Behavior

SWR is clearing the error data from cache on render even when the key is null, which should signal SWR not to try and fetch at all. When a user signs in to my app, I save their token to a cookie (and to react state) and the presence of the token tells SWR to perform a fetch. When this comes back with a server error, I need to display that error to the user and also clear their token from state and cookie. However, clearing the token triggers SWR to delete the cached error, so no error is displayed to the user.

Expected Behavior

If a render tells SWR not to fetch, don’t delete old data/errors.

Repro Steps / Code Example

Here is a CodeSandbox. Try adding some text and clicking “Log In”. You won’t see any error message. If you comment out line 12 where I clear the token, you will see an error message.

Additional Context

SWR version ^0.3.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
huozhicommented, Oct 19, 2020

hi all, bump this thread again. we reverted the behavior that let swr keep the error while revalidating. it’s published in 0.3.6, plz upgrade to get this issue fixed. thanks

1reaction
huozhicommented, Oct 17, 2020

I found these changes here: https://github.com/vercel/swr/pull/569/files#diff-755ef1ecc6f2f4c1cc601c6091dfcba5f674c653863b70913a115c0856a7b632R339-R343

when a hook is revalidating the state, it cleans the data / error of that key first (seems to make sense for me because we might want to clean up the states for last round revalidation).

IMO, if you’d like to preseve the error I think creating another hook to cache it in onError callback could achieve it, or use a useRef hook to compare with latest error returned from useSWR and update the ref when error changes and isn’t nullable. this could be a short-term workaround.

we’ll think of the more proper behavior of this case. thanks for reporting

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js TypeError although component not rendered
Why is this error showing up even though IndexPage does not get rendered? > Because it's breaking, you have an error, it won't...
Read more >
react-hydration-error - Next.js
Why This Error Occurred. While rendering your application, there was a difference between the React tree that was pre-rendered (SSR/SSG) and the React...
Read more >
Error States with ErrorBoundaries - Relay
Relay guide to rendering error states. ... Version: Next. On this page ... When retry is called, we will clear the error, and...
Read more >
FAQs | React Hook Form - Simple React forms validation
This approach reduces the amount of re-rendering that occurs due to a user ... will reset all field values, and will also clear...
Read more >
Rendering and Updating Data using Component Lifecycle ...
One of the crucial phases of the component, error handling is used to trigger an action in case of an error during the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found