添加一个私有路由,当用户未登录时,点击导航的首页或历史记录时,自动重定向到登录页面
const PrivateRoute = ({ children }) => {
const { UserStore } = useStore()
const { currentUser } = UserStore
return currentUser ? children : <Navigate to="/login" />
}
function App() {
return (
<>
<Header />
<main>
<Suspense fallback={<Loading />}>
<Routes>
<Route
path="/"
element={
<PrivateRoute>
<Home />
</PrivateRoute>
}
/>
<Route path="/about" element={<About />} />
<Route
path="/history"
element={
<PrivateRoute>
<History />
</PrivateRoute>
}
/>
<Route path="/login" element={<Login />} />
<Route path="/register" element={<Register />} />
</Routes>
</Suspense>
</main>
<Footer />
</>
)
}