T
TanStack2y ago
skilled-lime

Layout with redirect to child route

Hey! I'm in the simple situation with a /layout route with some html and two child /layout/a and /layout/b I'd like to set a redirect on the parent /layout to the child /layout/a, how can I do that?
5 Replies
frail-apricot
frail-apricot2y ago
// src/routes
index.tsx
foo/
index.tsx
a.tsx
b.tsx
// src/routes
index.tsx
foo/
index.tsx
a.tsx
b.tsx
// foo/index.tsx
export const Route("......")({
loader: () => {
throw redirect({ to: '/foo/a', replace: true })
}
})
// foo/index.tsx
export const Route("......")({
loader: () => {
throw redirect({ to: '/foo/a', replace: true })
}
})
fascinating-indigo
fascinating-indigo2y ago
Why are you redirecting via loader instead of beforeLoad? Isn't the loader of foo also called when foo/a is visited?
frail-apricot
frail-apricot2y ago
Its a matter of preference. I tend to have other checks happening in my beforeLoad callback, so I do my actual "work" for the route in the loader whilst leaving stuff like preflight-checks to be done in the beforeLoad callback. It shouldn't really matter in the grand-scheme of things.
fascinating-indigo
fascinating-indigo2y ago
Ok, thank you @Sean Cassiere. I tested it, it's a really good solution.👍🏽
skilled-lime
skilled-limeOP2y ago
Thank you 😄

Did you find this page helpful?