总是从应用程序中的任何路线刷新时重定向到主页

Always getting redirected to homepage on refresh from any route in app

我正在使用功能组件,它使用服务器端身份验证和本地存储为特定路由提供身份验证,例如 /home,发生在我的登录页面的 OnSubmit 事件中。

一切正常我在成功登录后被重定向到主页,当我从任何路径刷新页面时出现问题我总是被重定向到主页,为什么会这样?

问题出现在生产以及开发中,任何人都请建议我解决这个问题的方法

我的App.js文件是

App.js

import React, { useState, useEffect } from 'react';
import Swal from 'sweetalert2';
import {
  BrowserRouter as Router,
  Route,
  Switch,
  Redirect,
} from 'react-router-dom';

import LoginPage from './login/LoginPage';
import HomePage from './home/HomePage';
import Courses from './courses/Courses';
import Profile from './profile/Profile';
import Notes from './notes/Notes';
import Contact from './contact/Contact';
import Students from './students/Students';
import OnlineClass from './onlineClass/OnlineClass';
import NotFound from './notfound/NotFound';
import UpdateClass from './updateData/UpdateClass';
import UpdateNotes from './updateData/UpdateNotes';

const App = () => {
  const [loggedIn, setLoggedIn] = useState(false);

  useEffect(() => {
    const loggedUser = localStorage.getItem('user');
    if (loggedUser) {
      setLoggedIn(true);
    }
  }, []);

  const handleLoggedIn = () => {
    setLoggedIn(true);
  };

  const handleLogout = () => {
    Swal.fire({
      title: 'Are you sure?',
      showDenyButton: true,
      showCancelButton: true,
      confirmButtonText: `Yes`,
      denyButtonText: `No`,
    }).then((result) => {
      if (result.isConfirmed) {
        setLoggedIn(false);
        localStorage.clear();
      } else if (result.isDenied) {
        Swal.fire('Welcome back!', '', 'info');
      }
    });
  };

  return (
    <div>
      <Router>
        <Switch>
          <Route exact strict path='/'>
            {!loggedIn ? (
              <LoginPage handleLoggedIn={handleLoggedIn} />
            ) : (
              <Redirect to='/home' />
            )}
          </Route>
          <Route exact path='/home'>
            <HomePage handleLogout={handleLogout} loggedIn={loggedIn} />
          </Route>
          <Route exact path='/profile'>
            <Profile loggedIn={loggedIn} handleLogout={handleLogout} />
          </Route>
          <Route exact path='/courses'>
            <Courses loggedIn={loggedIn} handleLogout={handleLogout} />
          </Route>
          <Route exact path='/notes'>
            <Notes loggedIn={loggedIn} handleLogout={handleLogout} />
          </Route>
          <Route exact path='/contact'>
            <Contact loggedIn={loggedIn} handleLogout={handleLogout} />
          </Route>
          <Route exact path='/students'>
            <Students loggedIn={loggedIn} handleLogout={handleLogout} />
          </Route>
          <Route exact path='/class'>
            <OnlineClass loggedIn={loggedIn} handleLogout={handleLogout} />
          </Route>

          <Route exact path='/class/update/:id'>
            {!loggedIn ? (
              <LoginPage handleLoggedIn={handleLoggedIn} />
            ) : (
              <UpdateClass />
            )}
          </Route>
          <Route exact path='/notes/update/:id'>
            {!loggedIn ? (
              <LoginPage handleLoggedIn={handleLoggedIn} />
            ) : (
              <UpdateNotes />
            )}
          </Route>
          <Route>
            <NotFound />
          </Route>
        </Switch>
      </Router>
    </div>
  );
};

export default App;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

问题是setLoggedIn(true);是在重定向动作之后执行的:

您需要检查用户是否已加载。您可以将 loggedIn 初始化为 null

const [loggedIn, setLoggedIn] = useState(null);

useEffect(() => {
    const loggedUser = localStorage.getItem('user');
    setLoggedIn(Boolean(loggedUser));
  }, []);

如果loggedIn === null

那么你可以忽略渲染路由
return (
    <div>
      { loggedIn !== null &&
         <Router>
            ...
         </Router>
      }

    </div>
  );