How to perform redirects from the root loader? #304
Replies: 3 comments 2 replies
-
For future reference, this is how I would solve it:
Based on that, the loader will determine if it's authenticated and will load the redirection inside your app or the error component that will redirect to the login. A more amicable solution would be to do it inside the loader, as the documentation suggest (example below), but there is currently a circular dependency that needs to be resolved. The documentation does not give an example on how to do it so this is my best guess
|
Beta Was this translation helpful? Give feedback.
-
I need a way to immediately force redirect(no auth), from |
Beta Was this translation helpful? Give feedback.
-
I have an OK workaround for the circular dependency issue. I'm open to suggestions on a better pattern. Create a new file with an object to store a reference to the // navigate.ts
import type { router } from './main' // The circularity isn't a problem because it's just a type
/** Navigate imperatively.
* Cannot be used before router has been created (avoid using during app initialization/bootstrap phase). */
export const navigate = {
/* HACK: avoids circular dependency */
current: (() => {
throw Error('`navigate.current` was called before router was created')
}) as unknown as (typeof router)['navigate'],
} Set a reference to // main.js
import { navigate } from './navigate'
// ...
export const router = createRouter({
routeTree,
})
navigate.current = router.navigate // set the navigate function here Use |
Beta Was this translation helpful? Give feedback.
-
Hello all first time user of
@tanstack/location
and I'm having a bit of a disconnect in understanding what I am calling theroot loader
(top-level loader in the routes array). I am using@tanstack/react-query
as well and would like to utilize this root loader for:I understand that I can use
useMatch
to get reference to the data from the root loader in all the children routes. What I am not quite clear on is how to perform a redirect either out of the application if the user credentials are not present or how to redirect them through to another route.Example
Thanks for any pointers on how to handle this! Really liking this package already 🎉
Beta Was this translation helpful? Give feedback.
All reactions