Shallow Routing (prevent reload) #1712
Replies: 3 comments 1 reply
-
Whenever, your URL changes, it's considered to be a new route entry as it is in the browser. You can ofc. track this and choose to short-circuit the loader, but that'd be outside the purview of Router. |
Beta Was this translation helpful? Give feedback.
-
With a bit of trial and error and eventual realization this was documented, I managed to emulate this. I am using file based routing, and achieved this by having the root route defined as a named file outside of the sub-directory, e.g.: Doesn’t work:
Works:
Also works:
Then just render the same component in both routes. This way, navigating to/from root and children seems to keep the component mounted. That said, I am not sure rendering the same component in nested routes is stable library behavior... so what seems to be a more proper way to achieve this, is to render your tabs in children routes entirely, and an |
Beta Was this translation helpful? Give feedback.
-
I think you could achieve this with a splat route and sessionStorage. I had a case where I needed to change the url without re-fetching data and came up with this solution. In your loader const isShallow = sessionStorage.getItem("isShallow") === "true";
sessionStorage.removeItem("isShallow");
// if it's shallow, return early
if(isShallow) {
return{
isShallow
}
}
// otherwise continue as per usual In your component const isShallow = Route.useLoaderData({ select: (s) => s.isShallow });
// this is useful if you use tanstack query and need to pass it to the enabled param in useQuery And finally the actual navigation <button
onClick={(e) => {
// dont forget this!
sessionStorage.setItem("isShallow", "true");
router.navigate({
to: "/chat/$",
params: { _splat: "853c2a18-b171-4065-936d-9d99140101bc" },
});
}}
>
click me
</button> |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there an easy way to prevent reloading of the entire route when changing a search param? My use case is keep track of which tab menu the user is on, but updating the URL via
Route.navigate
triggers a full-page load.Beta Was this translation helpful? Give feedback.
All reactions