You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
createRootRoute does not render the pending component on initial page visit #2182
I want to load data that is relevant for the whole page (e.g. user related data) on the first load of the page and use it through the whole application where it is needed.
For that scenario, a loader function is added to the root of the application (__root.tsx).
Now the data is properly loaded and can be used through the whole application by using the function useLoaderData of the exported root component.
The problem is that while the loader is not resolved, an empty page is displayed.
Instead of an empty page, a loading spinner (or something else) should be displayed.
To achieve this behavior, a pendingComponent is added to the root.
The problem is that the component added as pendingComponent is not rendered so that the page is still empty while loading the page/data.
Thanks for you answer.
After setting the suggested flag the pendingComponent is properly displayed.
But now I am a bit confused:
As a substitute i used the defaultPendingComponent while initializing the router via createRouter:
importReactfrom'react';importReactDOMfrom'react-dom/client';import{RouterProvider,createRouter}from'@tanstack/react-router';import{routeTree}from'./routeTree.gen';// Set up a Router instanceconstrouter=createRouter({
routeTree,defaultPendingComponent: ()=><h2>Main Pending</h2>});// Register things for typesafetydeclare module '@tanstack/react-router'{interfaceRegister{router: typeofrouter;}}constrootElement=document.getElementById('app')!;if(!rootElement.innerHTML){constroot=ReactDOM.createRoot(rootElement);root.render(<RouterProviderrouter={router}/>);}
This renders the pending component while the loader in __root.tsx is not resolved without using wrapInSuspense flag.
Do you know why that is?
This doesn't seem to work in Tanstack Start. I don't see the pending component as the loader is running. The favicon spins showing the route is loading, then it just loads the component.
Activity
luansevero commentedon Sep 7, 2024
Need to set the
wrapInSuspenseoption to trueSalzstangeSalamiBrief commentedon Sep 9, 2024
Thanks for you answer.
After setting the suggested flag the
pendingComponentis properly displayed.But now I am a bit confused:
As a substitute i used the
defaultPendingComponentwhile initializing the router viacreateRouter:This renders the pending component while the loader in
__root.tsxis not resolved without usingwrapInSuspenseflag.Do you know why that is?
TkDodo commentedon Nov 23, 2024
This code here shows that having
wrapInSuspense: trueis necessary for root routes:router/packages/react-router/src/Match.tsx
Lines 52 to 54 in 55a2727
So maybe it just needs documenting?
I’m not entirely sure why adding a
defaultPendingComponentchanges this. @schiller-manuel ?jakec-dev commentedon Mar 11, 2025
This doesn't seem to work in Tanstack Start. I don't see the pending component as the loader is running. The favicon spins showing the route is loading, then it just loads the component.
Note this includes fixing the typo
wrapISuspensetest: pin 24 community-reported issues fixed by the match-loading wor…