gpt4 book ai didi

javascript - 通过导航重定向后,React Router 不会加载新的页面组件

转载 作者:行者123 更新时间:2023-12-03 22:40:32 24 4
gpt4 key购买 nike

我是 React Hooks 的新手并尝试使用它们 + 实验性的 React Router 版本 6 库,所以我不必在将来的某个时候将我的代码从版本 5 升级到 6

备注 : 版本 6 与版本 5 不兼容,所以没有 Switch不是答案;)

创意 :创建一个简单的管理应用程序,您可以在其中通过单击按钮登录/注销。管理页面受访问锁定保护,因此没有人可以通过直接 URL 访问它。

问题 : 点击登录按钮后,网址变为/admin ,但我看不到管理页面(仅在页面刷新或通过 windows.location.reload() 强制页面刷新后)。

这是 React Router v6 中的问题还是我使用 API 的方式错误?源代码( https://codesandbox.io/s/quirky-black-rsdy0 ):

import React, {useEffect, useState} from "react";
import {BrowserRouter, Route, Routes, useNavigate} from "react-router-dom";

async function isLoggedIn() {
// In real life this function is calling a REST endpoint and awaiting the result
return localStorage.getItem("loggedin") !== null;
}

async function setLoginStatus(status) {
// In real life this function is calling a REST endpoint and awaiting the result
if (status === true) {
localStorage.setItem("loggedin", true);
} else {
localStorage.removeItem("loggedin");
}
}

function LoginPage() {
const navigate = useNavigate();

async function handleLogin(event) {
event.preventDefault();
await setLoginStatus(true);
navigate("/admin"); // --> The user is logged in and the URL changes to /admin, but he doesn't see the admin page (Only after a page refresh)
//window.location.reload(); // Hack to reload the page to see the admin page
}

return (
<form onSubmit={handleLogin}>
<button type="submit">Login</button>
</form>
);
}

function LogoutPage() {
const navigate = useNavigate();

useEffect(() => {
const logoutAccount = async () => {
await setLoginStatus(false);
navigate("/login");
};
logoutAccount();
});

return <p>Logging out</p>;
}

function AdminPage() {
return (
<div>
<p>The secured admin page</p>
<a href="/logout">Logout</a>
</div>
);
}

function AuthenticationLock(props) {
if (props.isLoggedIn === true) {
return <div>{props.children}</div>;
} else {
return <LoginPage/>;
}
}

export default function App() {

const [loginStatus, setAppLoginStatus] = useState(false);

useEffect(() => {
const login = async () => {
const value = await isLoggedIn();
setAppLoginStatus(value);
};
login();
});

return (
<BrowserRouter>
<AuthenticationLock isLoggedIn={loginStatus}>
<Routes>
<Route path="/" element={<AdminPage/>}/>
<Route path="/admin" element={<AdminPage/>}/>
<Route path="/login" element={<LoginPage/>}/>
<Route path="/logout" element={<LogoutPage/>}/>
<Route path="/*" element={<AdminPage/>}/>
</Routes>
</AuthenticationLock>
</BrowserRouter>
);
}

最佳答案

不知怎的,我的 AuthenticationLock是造成问题的原因。我换了一个不同的设计,现在它可以工作了:

import React, { useEffect } from "react";
import {
BrowserRouter,
Route,
Routes,
Link,
useNavigate
} from "react-router-dom";

function isLoggedIn() {
// In real life this function is calling a REST endpoint and awaiting the result
return localStorage.getItem("loggedin") !== null;
}

async function setLoginStatus(status) {
// In real life this function is calling a REST endpoint and awaiting the result
if (status) {
localStorage.setItem("loggedin", "1");
} else {
localStorage.removeItem("loggedin");
}
}

function DashboardPage() {
return (
<div>
<h1>Dashboard page</h1>
<p>This is the dashboard page</p>
<Link to={"/accounts"}>Accounts</Link>
<br />
<Link to={"/logout"}>Logout</Link>
</div>
);
}

function AccountsPage() {
return (
<div>
<h1>Accounts page</h1>
<p>This is the accounts page</p>
<Link to={"/dashboard"}>Dashboard</Link>
<br />
<Link to={"/logout"}>Logout</Link>
</div>
);
}

function LoginPage() {
const navigate = useNavigate();

async function handleLogin(event) {
event.preventDefault();
await setLoginStatus(true);
navigate("/dashboard");
}

return (
<div>
<h1>Login</h1>
<form onSubmit={handleLogin}>
<button type="submit">Login</button>
</form>
</div>
);
}

function LogoutPage() {
const navigate = useNavigate();

useEffect(() => {
const logoutAccount = async () => {
await setLoginStatus(false);
navigate("/login");
};
logoutAccount();
});

return <p>Logging out</p>;
}

const PrivateRoute = ({ component, ...rest }) => {
if (isLoggedIn()) {
const routeComponent = props => React.createElement(component, props);
return <Route {...rest} render={routeComponent} />;
} else {
return <LoginPage />;
}
};

function App() {
return (
<BrowserRouter>
<Routes>
<PrivateRoute path="/" element={<DashboardPage />} />
<PrivateRoute path="/dashboard" element={<DashboardPage />} />
<PrivateRoute path="/accounts" element={<AccountsPage />} />
<Route path="/login" element={<LoginPage />} />
<PrivateRoute path="/logout" element={<LogoutPage />} />
<PrivateRoute path="/*" element={<DashboardPage />} />
</Routes>
</BrowserRouter>
);
}

export default App;

关于javascript - 通过导航重定向后,React Router 不会加载新的页面组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62300809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com