英文:
React Private route componente is undefined
问题
I want to load a component if a valid JWT exists, otherwise it should redirect to the login page. The redirection works, but if I have a valid token, the private component cannot be loaded and is undefined.
index.js
import React, { lazy, Suspense } from "react";
import ReactDOM from "react-dom/client";
import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
import jwtCheck from "./utils/jwtCheck"; // <-- returns true/false
import Loader from "./Loader";
const App = lazy(() => import("./App/App"));
const Login = lazy(() => import("./Login/Login"));
const root = ReactDOM.createRoot(document.getElementById("app"));
function PrivRoute({ child }) {
console.log("child:", child); // <-- "child: undefined"
return jwtCheck() ? child : <Navigate to="/login" />;
}
root.render(
<React.StrictMode>
<BrowserRouter>
<Suspense fallback={<Loader />}>
<Routes>
<Route
path="/"
element={
<PrivRoute>
<App />
</PrivRoute>
}
/>
<Route path="/login" element={<Login />} />
</Routes>
</Suspense>
</BrowserRouter>
</React.StrictMode>
);
Why is this happening? Can someone please help me? (This is my first "real" project with React)
英文:
I want to load a componente if a valid jwt exists, otherwise it should redirect to login, the redirect works but if I have a valid token the private component can not be loaded. is undefined
index.js
import React, { lazy, Suspense } from "react";
import ReactDOM from "react-dom/client";
import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
import jwtCheck from "./utils/jwtCheck"; // <-- return true/false
import Loader from "./Loader";
const App = lazy(() => import("./App/App"));
const Login = lazy(() => import("./Login/Login"));
const root = ReactDOM.createRoot(document.getElementById("app"));
function PrivRoute({ child }) {
console.log("child:", child); // <-- "child: undefined"
return jwtCheck() ? child : <Navigate to="/login" />;
}
root.render(
<React.StrictMode>
<BrowserRouter>
<Suspense fallback={<Loader />}>
<Routes>
<Route
path="/"
element={
<PrivRoute>
<App />
</PrivRoute>
}
/>
<Route path="/login" element={<Login />} />
</Routes>
</Suspense>
</BrowserRouter>
</React.StrictMode>
);
Why is this so, can someone please help me. (is my first "real" project with react)
答案1
得分: 1
The child of a component is found under the prop children
not child.
updated code:
function PrivRoute({ children: child }) {
console.log("child:", child); // <-- "child: undefined"
return jwtCheck() ? child : <Navigate to="/login" />;
}
英文:
The child of a component is found under the prop children
not child.
updated code:
function PrivRoute({ children: child }) {
console.log("child:", child); // <-- "child: undefined"
return jwtCheck() ? child : <Navigate to="/login" />;
}
答案2
得分: 0
尝试将 PrivRoute
中的 "chid" 更改为 "children":
function PrivRoute({ children }) {
console.log("child:", children); // <-- "child: undefined"
return jwtCheck() ? children : <Navigate to="/login" />;
}
英文:
try to change chid to children in PrivRoute
:
function PrivRoute({ children }) {
console.log("child:", children); // <-- "child: undefined"
return jwtCheck() ? children : <Navigate to="/login" />;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论