T
TanStack16mo ago
harsh-harlequin

Auto-redirect when accessing a route

Hey everyone, quick question: how do I automatically redirect a user from my-url.com/courses to my-url.com/courses/in-progress? Maybe I could use the beforeLoad option?
3 Replies
fascinating-indigo
fascinating-indigo16mo ago
// courses/index.jsx
import { createFileRoute, redirect } from "@tanstack/react-router";

export const Route = createFileRoute("/")({
loader: () => {
throw redirect({ to: "/courses/in-progress", replace: true });
},
});
// courses/index.jsx
import { createFileRoute, redirect } from "@tanstack/react-router";

export const Route = createFileRoute("/")({
loader: () => {
throw redirect({ to: "/courses/in-progress", replace: true });
},
});
harsh-harlequin
harsh-harlequinOP16mo ago
ty I was using
throw redirect({ to: "/courses/in-progress", replace: true });
throw redirect({ to: "/courses/in-progress", replace: true });
but in beforeLoad so its better to use in loader? gonna replace it
exotic-emerald
exotic-emerald16mo ago
throwing in beforeLoad is perfectly fine

Did you find this page helpful?