I'm trying to build a private and public route in React, and I'm using the useRoutes hook. I didn't know how to do this, already exist an answer to this question on Stack Overflow. However, it was just a copy and paste of some code. I really want to really learn how I can afford this.
I tried using the other answer, but it didn't help me much. If anyone can assist, I would be extremely grateful. This is the code I wrote.
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