React私有路由组件未定义。

huangapple go评论50阅读模式
英文:

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 &quot;react&quot;;
import ReactDOM from &quot;react-dom/client&quot;;
import { BrowserRouter, Routes, Route, Navigate } from &quot;react-router-dom&quot;;
import jwtCheck from &quot;./utils/jwtCheck&quot;; // &lt;-- return true/false

import Loader from &quot;./Loader&quot;;
const App = lazy(() =&gt; import(&quot;./App/App&quot;));
const Login = lazy(() =&gt; import(&quot;./Login/Login&quot;));


const root = ReactDOM.createRoot(document.getElementById(&quot;app&quot;));


function PrivRoute({ child }) {
	console.log(&quot;child:&quot;, child); // &lt;-- &quot;child: undefined&quot;
	return jwtCheck() ? child : &lt;Navigate to=&quot;/login&quot; /&gt;;
}


root.render(
	&lt;React.StrictMode&gt;
		&lt;BrowserRouter&gt;
			&lt;Suspense fallback={&lt;Loader /&gt;}&gt;
				&lt;Routes&gt;
					&lt;Route
						path=&quot;/&quot;
						element={
							&lt;PrivRoute&gt;
								&lt;App /&gt;
							&lt;/PrivRoute&gt;
						}
					/&gt;

					&lt;Route path=&quot;/login&quot; element={&lt;Login /&gt;} /&gt;
				&lt;/Routes&gt;
			&lt;/Suspense&gt;
		&lt;/BrowserRouter&gt;
	&lt;/React.StrictMode&gt;
);

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(&quot;child:&quot;, child); // &lt;-- &quot;child: undefined&quot;
    return jwtCheck() ? child : &lt;Navigate to=&quot;/login&quot; /&gt;;
}

答案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(&quot;child:&quot;, children); // &lt;-- &quot;child: undefined&quot;
return jwtCheck() ? children : &lt;Navigate to=&quot;/login&quot; /&gt;;
}

huangapple
  • 本文由 发表于 2023年2月16日 18:04:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定