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.

Component is re-rendered before leaving the page

See original GitHub issue

Hi 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 renders category.js page
  • I navigate back to /
  • the URL changes from /category?id=6 to /
  • before leaving category.js the component is re-rendered. Since the category.js page relies on the id parameter an error is thrown

Am I doing something wrong here?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Solitaryocommented, Apr 12, 2019

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?

0reactions
Bleuzencommented, Jul 12, 2020

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:

  const router = useRouter()
  const { id } = router.query

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:

let id

export default function Category() {
  const router = useRouter()

  if (router.pathname === '/category') {
    id = router.query.id
  }

...
}
Read more comments on GitHub >

github_iconTop 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 >

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