I am making a mern application with login/registration system and the dashboard and the other routes that will only be accessible when someone logs in now the problem is i was trying to write a if condition in react router dom so that all the routes inside the dashboard could be resitricted till the user logs in , and i use 'useNavigate' hook to go back to login page if user is not logged in but the application gives me error saying useNavigate() may be used only in the context of a component , but i used the same hook in my other component too where i didnt used any router and it worked fine there and so i am not able to understand what to do , also i want to know how can i put the component name inside a variable so that if i call the function i can put the component name in it and later put that varibale like this <component_name/> and it should change its value , here is my code:-
import Navbar from "./components/navbar/Navbar";
import Home from "./components/Home/Home";
import 'bootstrap/dist/css/bootstrap.min.css'
import Forgot_password from "./components/login_and_reg/Forgot_password/Forgot_password";
import Weather from "./components/Weather/Weather";
import Landing_page from './components/login_and_reg/Landing_page/Landing_page'
import {
BrowserRouter as Router,
Route,
Routes,
useNavigate
} from "react-router-dom";
import Verification from "./components/login_and_reg/Verification/Verification";
import Protected_routes from './components/Protected_routes'
import { useSelector } from "react-redux";
function App() {
const loggedin = useSelector(state => state.loggedin)
const Navigate = useNavigate();
const rememberMe = localStorage.getItem('rememberMe')
function checkroute(Component_name){
if (rememberMe=='true') {
return <Component_name/>
} else {
console.log(loggedin)
if(loggedin =='loggedin'){
return <Component_name/>
}
else{
Navigate('/')
}
}
}
return (
<>
<Router>
{/* <Navbar/> */}
<Routes>
<Route path="/weather" element={checkroute(Weather)}></Route>
<Route exact path="/" element={<Protected_routes/>}></Route>
<Route path="/verify/:first_name/:email" element={<Verification/>}></Route>
<Route path="/forgot_password" element={<Forgot_password/>}></Route>
{/* <Route exact path="/home" element={<Protected_routes/>}></Route> */}
</Routes>
</Router>
</>
);
}
I also made a protected route only for login purpose but i dont know how to use it for all the components if it is possible then here is code of that component:-
import React, { Component } from 'react';
import { useSelector } from 'react-redux';
import {Navigate, Route , useNavigate} from 'react-router-dom';
import Home from './Home/Home';
import Landing_page from './login_and_reg/Landing_page/Landing_page';
const Protected_routes = () => {
const loggedin = useSelector(state => state.loggedin)
const Navigate = useNavigate();
const rememberMe = localStorage.getItem('rememberMe')
if (rememberMe=='true') {
return <Home/>
} else {
if(loggedin=='loggedin'){
return <Home/>
}
else{
return <Landing_page/>
}
}
}
export default Protected_routes
export default App;