-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Re-rendering too often with suspense mode #454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
SWR will render one time without data, one with data as isValidating true and one with data and isValidating false. In development React will also render two times per every render, so you will get a total of 6 renders. Try it in production mode to check it renders 3 times. |
@sergiodxa Thanks so much for replying. In regards to the |
Ok about rerenders (and this may not have to do exactly with suspense)... But when you do a conditional fetch, once the key is valid you get 3 rerenders: |
Just wanted to put some thoughts on this matter out in the open... I see the same behavior and I struggle to understand why useSWR triggers revalidation in suspense mode. 1st render- throw to suspense while making request From these renders, I would think that useSWR made two requests (request on 1st render, and 2nd request on revalidation), but it actually makes only one (which is the correct behavior)... what I believe is wrong is the revalidation being triggered after the 2nd render. The most intuitive behavior and what one would expect while using Suspense would be the following: INITIAL RENDER: ... 3rd render- throw to suspense while making request I understand that it's supposed to return stale data while revalidating and having this behavior would throw SWR out of the picture, but when you're using the 'Suspense' feature, you expect to have it suspended when you have stale data (cache expired) or when you have no data in the cache. Personally, whenever I use suspense mode, I ignore the isValidating... you have the latest data on the 2nd render anyway... whatever happens after that is not reflective of the requests that are happening behind the scenes... To be honest I'm only using suspense mode with useSWR because I don't want to add another fetching library like |
Just dropping to add a +1 to this issue. I'm seeing 4 renders exactly as @mariopeixoto describes. I've tried this both in dev and production modes and see the same behavior. I'd love to use suspense mode, but can't easily deal with the multiple renders issue. |
I've created a PR to fix this #979 |
Uh oh!
There was an error while loading. Please reload this page.
It seems like re-renders are happening too frequently when suspense mode is enabled.
Expected Behavior
There should be one render to kick off the request, and then another render when the data is loaded.
Actual Behavior
The component re-renders a total of 6 times. 4 times, the component throws to suspense. One more time after it has received the data. And then one more time for reasons I am unsure of.
Here is a minimum reproducible Sandbox.
The text was updated successfully, but these errors were encountered: