router icon indicating copy to clipboard operation
router copied to clipboard

Double Renders On Navigate

Open ReeceHughes opened this issue 2 years ago • 7 comments

Describe the bug

Components are rendered twice when navigating to them. I would expect that a component is only rendered once. This reproduction code is the file based quickstart example from the website with the following modifications:

  • Added console.logs in the Root, Index, and About components.
  • Disable pre-loading (the bug reproduces with it enabled or disabled but it is clearer to see the logging with pre-loading disabled)

Issue originally raised from this discussion.

Your Example Website or App

https://stackblitz.com/edit/github-mg7svm?file=src%2Froutes%2Fabout.tsx

Steps to Reproduce the Bug or Issue

  1. Clear the console.
  2. Click on Home in the navigation bar
  3. Click on About

Observe the console output, each component and the root component is re-rendered. Example Output:

Root
Home
Root
Home
Root
About
Root
About

Expected behavior

I expected the root component to be rendered once total and the children (home and about) to only be rendered 1 time when they are navigated to.

Screenshots or Videos

No response

Platform

  • OS: Linux
  • Browser: Brave/Chromium
  • Version: Version 1.67.116 Chromium: 126.0.6478.71

Additional context

No response

ReeceHughes avatar Jun 24 '24 21:06 ReeceHughes

We don't memoize your components. We follow React's rendering.

Same thing, with the memoization: https://stackblitz.com/edit/github-mg7svm-ed3yqq?file=src%2Froutes%2F__root.tsx

SeanCassiere avatar Jun 25 '24 11:06 SeanCassiere

Getting same double rendering issue in my project

We don't memoize your components. We follow React's rendering.

Same thing, with the memoization: https://stackblitz.com/edit/github-mg7svm-ed3yqq?file=src%2Froutes%2F__root.tsx

Not able to open this link, i added memoization too, but just want to check it.

nimish-gupta avatar Sep 12 '24 07:09 nimish-gupta

@nimish-gupta can you please open a new issue and provide a minimal complete example by forking one of the examples on stackblitz?

schiller-manuel avatar Sep 12 '24 20:09 schiller-manuel

In case this helps anyone still seeing this issue, I found that having StrictMode as shown in the docs, it rerenders twice in dev but DOES NOT in prod build.

if (rootElement) {
  const root = ReactDOM.createRoot(rootElement);
  root.render(
    <StrictMode>
      <RouterProvider router={router} />
    </StrictMode>,
  );
}

abartolo avatar Jan 15 '25 21:01 abartolo

I also had this issue, but it was user error in my case. I was accidentally rendering <Outlet /> twice in my root component due to a quick and messy migration to tanstack-router. Worth taking small moments to sanity check things to potentially save hours of headache later.

toph-pottschmidt avatar Apr 14 '25 19:04 toph-pottschmidt

@toph-pottschmidt Thank you - I was loosing my mind over this and thanks you to I found out I had the exact same issue in my code.

lmyslinski avatar Jan 18 '26 17:01 lmyslinski

In case this helps anyone still seeing this issue, I found that having StrictMode as shown in the docs, it rerenders twice in dev but DOES NOT in prod build.

if (rootElement) {
  const root = ReactDOM.createRoot(rootElement);
  root.render(
    <StrictMode>
      <RouterProvider router={router} />
    </StrictMode>,
  );
}

That means it is rendered via a React.useEffect, that gets called twice in StrictMode. Would be easy to fix on TanstackDevTool's side. I can confirm, once I drag the devtools outside the StrictMode context it works as intended. Good find @toph-pottschmidt!

krnlde avatar Mar 20 '26 12:03 krnlde