Unnecessary rendering on data re-fetching
See original GitHub issueDescribe the bug
I was using why-did-you-render
in local and noticed a bunch of unnecessary re-renders happening because of something related to react-query
that goes beyond my power as a lib consumer.
To Reproduce
- Go to https://next-auth-1b8mmpq6w.vercel.app/
- Check console.
- Switch tab back and forth to regain window focus.
- Check console again to see unnecessary re-renders happening.
Expected behavior It should not re-render if data is the same.
Additional context
- It’s a Next.js based demo, here’s the codebase.
- If you go to https://next-auth-1b8mmpq6w.vercel.app/swr you are going to see the same thing but handled by SWR. There are not re-renders on stale re-fetching that returns the same data.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Top Results From Across the Web
Unnecessary rendering on data re-fetching · Issue #938
I was using why-did-you-render in local and noticed a bunch of unnecessary re-renders happening because of something related to react-query that ...
Read more >5 Ways to Avoid React Component Re-Renderings
How to Avoid Unnecessary Re-rendering in React ... It's common to use the useEffect() Hook for asynchronous data fetching operations in React applications....
Read more >React Query Render Optimizations - TkDodo's blog
An advanced guide to minimize component re-renderings when using React Query.
Read more >How to prevent re-rendering of components that have not ...
I would like to prevent the re-rendering and only re-render the component that actually changes. I have seen that useCallback is the right...
Read more >Optimizing React performance by preventing unnecessary ...
Only re-rendering React components when necessary can make app your faster. This article explains how to reduce re-renders and avoid common ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi @ankursehdev! Yes in both cases the additional renders are because the query is being revalidated on window focus. You can either disable this behaviour with
refetchOnWindowFocus
or wait for https://github.com/tannerlinsley/react-query/pull/840 to be merged which will allow you to keep the revalidation, but prevent additional renders.I am facing a similar issue too, I have few components on a page - Select Dropdown being one. If I click that dropdown the query is trying to fetch the data again. I don’t know why.
Even on this demo, if you click somewhere post request is firing, is it the right behaviors? Check the GIF below.
I wonder, is it because I am losing focus on the tab and regaining it once I click back? I am new to react-query actually.