router object inside the beforeLoad method #806
-
import { Route, redirect, useRouter } from "@tanstack/react-router";
import { adminLayout } from "./AdminLayout";
export const adminIndexRoute = new Route({
getParentRoute: () => adminLayout,
path: "/",
beforeLoad: async() => {
if(localStorage.getItem("admin") === null) {
throw redirect({
to: "/auth",
search: {
// Use the current location to power a redirect after login
// (Do not use `router.state.resolvedLocation` as it can
// potentially lag behind the actual current location)
// redirect: router.state.location.href,
redirect: "/",
},
});
}
},
component: ({ }) => {
const router = useRouter()
return (
<div className="w-full h-full flex items-center justify-center">
<h3>Admin only section</h3>
<button
onClick={() => {
localStorage.removeItem("admin");
router.navigate({to: "."});
}}
className="btn btn-sm">
Logout
</button>
</div>
);
},
}); is there a way to access the router properties like pathname inside the beforeLoad method since the useRouter can't be used there |
Beta Was this translation helpful? Give feedback.
Answered by
Angusross111
Nov 19, 2023
Replies: 1 comment 1 reply
-
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can access the location object in the beforeLoad function