How to redirect to a default child route?
What's the best way to automatically redirect from a parent route to a default child route?
Let's say we have an
editorRoute:
and some child routes including this one:
All the routes under /editor use the layout declared in the Editor component.
Since /editor path by itself does not display anything meaningful, I want it to automatically redirect to /editor/rooms.
What would be the best way and place to do that?13 Replies
like-gold•2y ago
create a child route with
path: '/' and redirect in beforeLoad with throw redirect 🙂conscious-sapphire•2y ago
you can create an index route like
environmental-roseOP•2y ago
Awesome! Thanks!
sensitive-blue•12mo ago
Having a similar issue and we're using file based routing. Could use some help, I'm sure it's something simple but spending way too much time on it.
/dashboard itself doesn't have content, so we redirect based on role... but this redirects for every child route when navigating directly to it or refreshing (i.e. /dashboard/invoices will redirect to /requests for regular users). Thank you so much!
evident-indigo•12mo ago
same solution as above.create an index route and throw the redirect there
sensitive-blue•12mo ago
Apologies if I'm being obtuse (likely the case), the route I posted is our index route. It's _auth.dashboard.index.tsx
evident-indigo•12mo ago
right I didn't notice that
sorry
can you please provide a minimal complete example?
on Stackblitz ideally
sensitive-blue•12mo ago
Sure thing will do! Thanks very much, Manuel!
evident-indigo•12mo ago
the redirect being thrown in the index route should not affect children
as the index is a leaf
sensitive-blue•12mo ago
Wow, sorry for wasting your time, but thanks for suggesting a minimal repro. Could not repro.
It turns out it was my redirect handling on the child route which is based on authenticated user state. I was not returning early if the user state wasn't defined yet, so it fell through to the default redirect to /requests.
Have a wonderful day! Just wanted to say I appreciate everything you folks are doing with these libraries. They're great to work with and make dev life much easier. 🙂
evident-indigo•12mo ago
glad you found your issue
blank-aquamarine•11mo ago
with fileRouter the file route the index route is loaded with child routes as well - so it keeps throwing. Is there another way to do this?
evident-indigo•11mo ago
unclear to me what you mean. can you please create a complete minimal example (e.g. by forking one of the existing examples on stackblitz)?