router icon indicating copy to clipboard operation
router copied to clipboard

I'm reaching out to report a performance issue.

Open AndrejNemec opened this issue 10 months ago • 2 comments

I'm reaching out to report a performance issue.

The problem involves performance in two scenarios:

  1. When updating the search parameters using useNavigation, the entire page, including all layout components and child components, is re-rendered.
  2. When there is a layout component with multiple child pages, navigating between these child pages triggers a re-render of the entire page, including all layout components.

In the classic react-router package, this problem does not exist, there it works as expected, parts are rerendered as expected.

Example Link: https://stackblitz.com/edit/vitejs-vite-ifxs98

Originally posted by @AndrejNemec in https://github.com/TanStack/router/discussions/1460

AndrejNemec avatar Apr 12 '24 13:04 AndrejNemec

Do we have an update on this? Also having this issue.

divmgl avatar Apr 24 '24 19:04 divmgl

Wrapping all layouts with React.memo can fix this issue for now.

createdbymahmood avatar Jun 13 '24 22:06 createdbymahmood

@tannerlinsley do you know how this issue could be fixed?

AO19 avatar Jul 18 '24 09:07 AO19

The latest version may have fixed it. We improved performance quite a bit with some more fine grained selectors

tannerlinsley avatar Jul 18 '24 16:07 tannerlinsley

The latest version may have fixed it. We improved performance quite a bit with some more fine grained selectors

It didnt'. I'm on v1.45.6 and I'm running into the same problem.

Here's code on how to reproduce it. The onChange fires updateQuery which re-render the entire app. https://github.com/TanStack/router/discussions/1993#discussion-6952871

decipher-cs avatar Jul 19 '24 14:07 decipher-cs

Not sure if it's totally related, but I had some performance issues when dealing with URL hash updates when using the Default Browser History as mentioned here: https://github.com/TanStack/router/issues/1999

gbbrambila avatar Jul 20 '24 09:07 gbbrambila

We are also having this issue. we migrated to tanstack from react router and found this problem very annoying. this kind of issue is not happening with react routers. we are thinking of rolling back to react router because of this issue.

@last-Programmer That's not the way to communicate. Please show some respect for people who put time and effort into open source. There is already a workaround using React.memo, and you should use it until it's fixed, or go back to whatever you want to use.

tunganh-phamba avatar Jul 24 '24 09:07 tunganh-phamba

Not sure this is exactly related but on the React Example: Basic & Basic File Based (haven't tested others), on every url change, the RootComponent re-render twice. Was struggling with this and after putting memo and useCallback everywhere without success, I'm glad to find out it could be the issue. Versions tested: 1.45.7 & 1.45.11

trompx avatar Jul 27 '24 00:07 trompx

I just upgraded to 1.45.14. While there was a double rerender in my root component when using const router = useRouter() there is now none at all, so it breaks my app.

Is it expected that we get no rerender at all now on new url navigation and should use another hook for a single rerender?

trompx avatar Jul 30 '24 13:07 trompx

@trompx can you please share a complete minimal example?

schiller-manuel avatar Jul 30 '24 14:07 schiller-manuel

@schiller-manuel

  1. Go to https://tanstack.com/router/latest/docs/framework/react/examples/basic
  2. Import useRouter and add the following at the beginning of the function RootComponent() {:
  const router = useRouter();
  console.log("ROOT", router)
  1. Check console: only renders "ROOT" on the first page

trompx avatar Jul 30 '24 14:07 trompx

useRouter is not reactive, we need to document this better. use useRouterState instead: https://stackblitz.com/edit/tanstack-router-m1avwu?file=src%2Fmain.tsx&preset=node

schiller-manuel avatar Jul 30 '24 16:07 schiller-manuel

useRouter is like useQueryClient. Somehow, there most people understand that it's not reactive. Maybe it should be useRouterInstance or something?

TkDodo avatar Jul 30 '24 18:07 TkDodo