I need two sets of routing in my app. One at the top level which routes between the different pages that don't need authentication and another set inside the authenticated area. Here's my top level route component.
function App() {
return (
<Router>
<Routes>
<Route path='/' element={<ProtectedRoute/>}>
<Route path='/' element={<Dashboard>
</Dashboard>}/>
</Route>
<Route exact path="/sign-up" element={<SignUpPage/>}/>
<Route exact path="/login" element={<LogInPage/>}/>
<Route exact path="/activate-user" element={<ActivateUserPage/>}/>
<Route exact path="/forgot-password" element={<ForgotPasswordPage/>}/>
<Route exact path="/reset-password" element={<ResetPasswordPage/>}/>
</Routes>
</Router>
);
}
export default App;
This is the protected route.
export const ProtectedRoute = () => {
const auth = localStorage.getItem("xyz");
return auth ? <Dashboard /> : <Navigate to="/login" />
}
This part works. Now I need a routing inside the Dashboard. Here's my dashboard component with the desired routing.
const Dashboard = () => {
return (
<nav>
<ul>
<li><Link to=""><AiOutlineHome/></Link></li>
<li><Link to=""><AiOutlineHistory/></Link></li>
<li><Link to=""><BsFillPlusCircleFill/></Link></li>
<li><Link to=""><BsChatDots/></Link></li>
<li><Link to=""><RiUserSettingsLine/></Link></li>
</ul>
</nav>
<section>
<Route path='/feeds' element={<HomePage/>}/>
<Route path='/capture' element={<CaptureStoryPage/>}/>
</section>
</div>
)
}
This doesn't work and the error that I get is this:
router.ts:5 Uncaught Error: A <Route> is only ever to be used as the child of <Routes> element, never rendered directly. Please wrap your <Route> in a <Routes>.
I'm new to React and could really use some help here. Thanks in advance.