Auto-redirect
similar to this other question, i want to auto redirect to a child route on nav -- i.e. when navigating to
settings
i want to redirect to settings/organizations
-- i tried the below along with several iterations and none can properly redirect (the nav routes work as expected)
6 Replies
ratty-blushOP•10mo ago
fwiw if someone else comes here, the below is a workaround but i'd love to figure out how the built in setup is uspposed to be
huh, i just tried my original example again and it seems to do the redirect in the url but it totally freezes the browser and doesn't functionally work
other-emerald•10mo ago
Here's the same solution using a beforeLoad. You need to check the location on entry for the pathname being "/settings" because if you don't essentially every route will encounter the redirect and cause it to get into an infinite redirect situation.
https://stackblitz.com/edit/tanstack-router-dumsifrg?file=src%2Froutes%2Fsettings.tsx
Eric Chernuka
StackBlitz
TSR Settings redirect - StackBlitz
Run official live example code for Router Kitchen Sink File Based, created by Tanstack on StackBlitz
sunny-green•10mo ago
is there any reason you can't use navigate inside ?
ratty-blushOP•10mo ago
That’s essentially what the hook does @Jay
Thanks @Eric Chernuka I missed your message before but sounds like a winner and I will give this a try!
other-emerald•10mo ago
If you wanted to do it in the component just do an early return of <Navigate />
ratty-blushOP•9mo ago
for reference in case that stack blitz dies: