Error: Redirecting from parent to child on load
I think what I'm doing has a better solution, but I'm not sure what to do. I have a folder structure that looks something like this:
I've added a redirect in
route.tsx that looks like this:
When I go to mywebsite.com/parent/child-1 it all works fine, but if I go to mywebsite.com/parent I want it to redirect to child-1.
The problem is that I can get it to redirect properly but none of the content from child-1.tsx or route.tsx is showing. The page is just blank.10 Replies
rare-sapphire•2y ago
You should use redirect inside beforeLoad function instead as it's a better integration. But it should work either inside loader I think
stormy-goldOP•2y ago
Yeah I've tried with
beforeLoad too and it errors:
A component suspended while responding to synchronous input. This will cause the UI to be replaced with a loading indicator. To fix, updates that suspend should be wrapped with startTransitionrare-sapphire•2y ago
does it work if you remove your parent component ?
stormy-goldOP•2y ago
The
route.tsx?rare-sapphire•2y ago
the component property in your createFileRoute
Ok I understand
you're actually throwing an redirect in a layout that is used in the redirect path. So you're permanently redirecting your app
What you want is redirecting to /child-1 if you're on the path /parent/, right ?
stormy-goldOP•2y ago
Yes!
rare-sapphire•2y ago
Then you should use the before load and redirect inside a file called index.tsx
For example.
if you're on the page /parent/child-1, then the files parent/route.tsx and child-1.index.tsx are called.
If you're on the page /parent, then the file parent/index.tsx is called
a child route is rendered inside the outlet of it's parent route. An Outlet cannot be used in an index route as it cannot have a child route.
stormy-goldOP•2y ago
Thank you for the explanation! It all makes sense when you start having the applicable problems 😄
rare-sapphire•2y ago
No problem !
stormy-goldOP•2y ago
Solution
In the Kitchen Sink example I found this:
and it took me a little while to realize that it translates to this:
With that realization I fixed my issue by doing this: