uncate typeerror: rootroute.addchildren не является функцией < /strong> < /p>
Я определил свой контекст и обернул мое приложение: < /p>
this.
Код: Выделить всё
import { AuthProvider, useAuth } from "./context/auth/auth.jsx";
import { router } from "./router/routes.jsx";
import { RouterProvider } from "@tanstack/react-router";
function App() {
return (
);
}
function InnerApp() {
const auth = useAuth();
return ;
}
export default App;
< /code>
Это routes.jsx для определения маршрутизатора < /p>
import {
Router,
Outlet,
createRoute,
redirect,
createRootRouteWithContext,
} from "@tanstack/react-router";
import HomePage from "../pages/HomePage";
import LoginPage from "../pages/LoginPage";
const rootRoute = createRootRouteWithContext({
component: () => (
),
});
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: "/",
component: HomePage,
});
const loginRoute = createRoute({
getParentRoute: () => rootRoute,
path: "/login",
component: LoginPage,
beforeLoad: ({ context }) => {
const authctx = context.auth;
const { role, isLoggedIn } = authctx();
if (isLoggedIn && role == "user") {
throw redirect({ to: "/" });
}
if (isLoggedIn && role == "admin") {
throw redirect({ to: "/admin" });
}
return;
},
});
const routeTree = rootRoute.addChildren([
indexRoute,
loginRoute,
]);
export const router = new Router({
routeTree,
context:{
auth:undefined
}
});
Подробнее здесь: https://stackoverflow.com/questions/796 ... te-with-js