Component is re-rendered before leaving the page
See original GitHub issueHi all 👋
I’d love to use this library in my project but I’m having an issue with route changes. When I leave a page that is re-rendered. This throws an error in some cases because the route has changed in the meantime and the component can’t find some route parameters.
Let me give you an example.
- I’m in the index
/
- I navigate to
/category?id=6
which renderscategory.js
page - I navigate back to
/
- the URL changes from
/category?id=6
to/
- before leaving
category.js
the component is re-rendered. Since thecategory.js
page relies on theid
parameter an error is thrown
Am I doing something wrong here?
Issue Analytics
- State:
- Created 5 years ago
- Comments:13 (3 by maintainers)
Top Results From Across the Web
When does React re-render components? - Felix Gerschau
React is known for providing a fast user experience by only updating the parts of the UI that have changed. When looking into...
Read more >How to prevent re-rendering of components that have not ...
Personally I would avoid React.memo / React.useRef / React.useCallback . The simplest solution to your example is just create another ...
Read more >5 Ways to Avoid React Component Re-Renderings
When we make an API call, React Query will first return the data from the cache before continuing with the request. Then, it...
Read more >How and when to force a React component to re-render
React automatically re-renders components, but what happens when a component is not updating as expected? Find out what you can do here.
Read more >React re-renders guide: everything, all at once - Developer way
Basically a component rerenders only when the parent rerenders, and the only reason for a parent to rerender is when state is changed?...
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 FreeTop 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
Top GitHub Comments
I have the same problem, I’m using Apollo and a url with query params, every time I go back from a URL with query params, the animation doesn’t work, it first load the page and then it does the animation, like re rendering the component first and then using the animation. But this only happens when you have URL params, which is weird. Any idea how to force the animation?
Also happens with framer-motion. The problem seems to be that next router already changes the route and query parameters before the exit animation finished.
So the old page gets the query parameters of the new route it is transitioning to.
Example:
If you use the state to get the query parameters:
the id parameter first is the id parameter passed to this page. But when you click on a link, next already changed the route and query parameters to the new page. So the id parameter becomes undefined, if the new route it is transitioning to does not have an id parameter. And since this is a state change, the old page which is fading out is now re-rendered with the new id parameter of the new route (which is undefined in this example).
Somehow next router route and query changes have to be delayed until the exit animation is done.
One workaround for now is to check the pathname before setting the id (and other query parameters you use) so that the old route doesn’t get the parameters of the new route. This may look something like this: