Last active
June 8, 2022 21:40
-
-
Save mfrancois3k/ca154cbfbfb4b385f62e391f628f0059 to your computer and use it in GitHub Desktop.
Protected-private-routes-react-router-dom-v6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Home() { | |
return <div>This is my Home Page</div>; | |
} | |
export default Home; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Navigate, Outlet } from "react-router-dom"; | |
function PrivateRoute({ isLogged }) { | |
return isLogged ? <Outlet /> : <Navigate to="/" />; | |
} | |
export default PrivateRoute; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Profile() { | |
return <div>THis is my Profile</div>; | |
} | |
export default Profile; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://github.com/syed-ashraf123/protected-private-routes-react-router-dom-v6/blob/master/src/App.js | |
import logo from "./logo.svg"; | |
import "./App.css"; | |
import Home from "./Home"; | |
import Profile from "./Profile"; | |
import PrivateRoute from "./PrivateRoute"; | |
import { BrowserRouter as Router, Routes, Route, Link } from "react-router-dom"; | |
import { useState } from "react"; | |
function App() { | |
const [isLogged, setIsLogged] = useState(false); | |
return ( | |
<div className="App"> | |
<button onClick={() => setIsLogged(true)}>LogIn</button> | |
<button onClick={() => setIsLogged(false)}>LogOut</button> | |
<Router> | |
<Routes> | |
<Route path="/" element={<Home />} /> | |
<Route element={<PrivateRoute isLogged={isLogged} />}> | |
<Route path="/profile" element={<Profile />} /> | |
</Route> | |
</Routes> | |
<Link to="/profile"> Got to Profile</Link> | |
</Router> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment