我正在try 在Reaction中构建一条私有和公共的路径,并且我正在使用useRoutes钩子.我不知道该怎么做,在堆栈溢出上已经有了这个问题的答案.然而,这只是一些代码的复制和粘贴.我真的很想知道我是怎么买得起这个的.
我试着用另一个答案,但它对我没有多大帮助.如果有人能帮忙,我将不胜感激.这是我写的代码.
import Home from "../pages/home"
import Items from "../pages/itens"
import Login from "../pages/login"
import Printers from "../pages/printers"
import { AuthContext } from "../contexts"
import { useContext } from "react"
import { Navigate } from "react-router-dom"
function Routes() {
const { sessionContextValue } = useContext(AuthContext)
const { isAuth } = sessionContextValue
const routes = [
{
path: '/',
element: isAuth ? <Home /> : <Navigate to={'/login'} replace/>,
},
{
path: '/printers',
element: <Printers />
},
{
path: '/items',
element: <Items />
},
{
path: '/login',
element: <Login />
}
]
let router = useRoutes(routes)
return router
}
export default Routes