Last active
February 20, 2022 04:47
-
-
Save mhaecal/556ae06b14477ee994c09e296a375f40 to your computer and use it in GitHub Desktop.
React Router 6
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 React from 'react' | |
import { Outlet } from 'react-router-dom' | |
export default function App() { | |
return <Outlet /> | |
} |
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 React from 'react' | |
function Home() { | |
return <div>This is homepage</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 React from 'react' | |
import { Outlet } from 'react-router-dom' | |
import TheNavbar from '../TheNavbar' | |
function MainLayout() { | |
return ( | |
<div> | |
<TheNavbar /> | |
<Outlet /> | |
</div> | |
) | |
} | |
export default MainLayout |
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 React from 'react' | |
import { useNavigate } from 'react-router-dom' | |
function Login() { | |
const navigate = useNavigate() | |
return ( | |
<div> | |
<span>This login page use different layout</span> | |
<p> | |
<input type="text" placeholder="Username" /> | |
</p> | |
<p> | |
<input type="password" placeholder="Password" /> | |
</p> | |
<button onClick={() => navigate('/')}>Go to Home</button> | |
</div> | |
) | |
} | |
export default Login |
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 React from 'react' | |
import ReactDOM from 'react-dom' | |
import { BrowserRouter, Route, Routes } from 'react-router-dom' | |
import App from './App' | |
import MainLayout from './components/layouts/MainLayout' | |
import Home from './pages/Home' | |
import Login from './pages/Login' | |
import ProductList from './pages/ProductList' | |
import ProductItem from './pages/ProductItem' | |
ReactDOM.render( | |
<React.StrictMode> | |
<BrowserRouter> | |
{/*start routes*/} | |
<Routes> | |
<Route element={<MainLayout />}> | |
<Route path="/" element={<Home />} /> | |
<Route path="/product" element={<ProductList />} /> | |
<Route path="/product/:id" element={<ProductItem />} /> | |
</Route> | |
<Route element={<App />}> | |
<Route path="/login" element={<Login />} /> | |
</Route> | |
</Routes> | |
{/*end routes*/} | |
</BrowserRouter> | |
</React.StrictMode>, | |
document.getElementById('root') | |
) |
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 React from 'react' | |
import { useParams } from 'react-router-dom' | |
function ProductItem() { | |
const params = useParams() | |
return ( | |
<div> | |
Product item <strong>{params.id}</strong> | |
</div> | |
) | |
} | |
export default ProductItem |
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 React from 'react' | |
import { Link } from 'react-router-dom' | |
function ProductList() { | |
return ( | |
<div> | |
<p>This is product page</p> | |
<ul> | |
{[1, 2, 3].map((id, index) => ( | |
<li key={index}> | |
<Link to={'/product/' + id}>Product {id}</Link> | |
</li> | |
))} | |
</ul> | |
</div> | |
) | |
} | |
export default ProductList |
Author
mhaecal
commented
Feb 20, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment