This React app uses React Router to switch between Home, About, and a dynamic Profile page. The navigation links let you move between pages without reloading.
import React from 'react';
import { createRoot } from 'react-dom/client';
import { BrowserRouter, Routes, Route, Link, useParams } from 'react-router-dom';
function Home() {
return <h2>Home Page</h2>;
}
function About() {
return <h2>About Page</h2>;
}
function Profile() {
const { id } = useParams();
return <h2>Profile Page for user {id}</h2>;
}
function App() {
return (
<BrowserRouter>
<nav aria-label="Main navigation">
<Link to="/" style={{ marginRight: '1rem' }}>Home</Link>
<Link to="/about" style={{ marginRight: '1rem' }}>About</Link>
<Link to="/profile/42">Profile 42</Link>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/profile/:id" element={<Profile />} />
</Routes>
</BrowserRouter>
);
}
const container = document.getElementById('root');
const root = createRoot(container);
root.render(<App />);