Dynamic Route

//============Main.js======

import React from "react";
import {BrowserRouter as Router,Link,Routes,Route} from "react-router-dom";

import HeaderNav from "./HeaderNav";

import Home from "./Home";
import About from "./About";
import Contact from "./Contact";
import PageNotFound from "./PageNotFound";
import Profile from "./Profile";

export default function () {

return<>
<Router>
<HeaderNav/>
<Routes>
<Route path={"/"} element={<Home/>}/>
<Route path={"/about"} element={<About/>}/>
<Route path={"/contact"} element={<Contact/>}/>
<Route path={"/User/:username"} element={<Profile/>}/>
<Route path={"*"} element={<PageNotFound/>}/>
</Routes>
</Router>
</>

} 




import React from "react";
import { useParams } from "react-router-dom";

export default function Profile() {
const { username } = useParams(); // Extract the dynamic username from the URL

return (
<>
<h1>Profile Page</h1>
<div>
<h2>Welcome, {username}!</h2>
<p>This is the profile page for <strong>{username}</strong>.</p>
</div>
</>
);
}




Comments

Popular posts from this blog

App 1st User Define Component

Example 1

Rabiit