Home
; }; export default Home; // About.js import React from 'react'; const About =()=>{ returnAbout
; }; export default About; // Contact.js import React from 'react'; const Contact =()=>{ returnContact
; }; export default Contact; ### 2. Set Up Routing In your main application component, use the components from react-router-dom to set up routing. ## Example import React from 'react'; import ReactDOM from 'react-dom/client'; import{ BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom'; import Home from './Home'; import About from './About'; import Contact from './Contact'; const App =()=>{ return(
<Route path="/" element={}/>
<Route path="/about" element={}/>
<Route path="/contact" element={}/>
);
};
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render();
## Nested Routing
React Router allows you to create nested routes, which can render more routes within a component.
## Example
// Dashboard.js
import React from 'react';
import{ Link, Outlet } from 'react-router-dom';
const Dashboard =()=>{
return(
Dashboard
Profile
; }; export default Profile; ## Example // Settings.js import React from 'react'; const Settings =()=>{ returnSettings
; }; export default Settings; Set up nested routes in the main application component: ## Example import React from 'react'; import ReactDOM from 'react-dom/client'; import{ BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom'; import Home from './Home'; import About from './About'; import Contact from './Contact'; import Dashboard from './Dashboard'; import Profile from './Profile'; import Settings from './Settings'; const App =()=>{ return(
<Route path="/" element={}/>
<Route path="/about" element={}/>
<Route path="/contact" element={}/>
<Route path="/dashboard" element={}>
<Route path="profile" element={}/>
<Route path="settings" element={}/>
);
};
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render();
## Dynamic Routing
You can include dynamic parameters in the path.
## Example
// User.js
import React from 'react';
import{ useParams } from 'react-router-dom';
const User =()=>{
const{ userId }= useParams();
returnUser ID:{userId}
; }; export default User; Set up dynamic routing in the main application component: ## Example import React from 'react'; import ReactDOM from 'react-dom/client'; import{ BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom'; import Home from './Home'; import About from './About'; import Contact from './Contact'; import Dashboard from './Dashboard'; import Profile from './Profile'; import Settings from './Settings'; import User from './User'; const App =()=>{ return(
<Route path="/" element={}/>
<Route path="/about" element={}/>
<Route path="/contact" element={}/>
<Route path="/dashboard" element={}>
<Route path="profile" element={}/>
<Route path="settings" element={}/>
<Route path="/user/:userId" element={}/>
);
};
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render();
## 404 Page
You can use the Navigate component provided by React Router to implement 404 page redirection.
## Example
import React from 'react';
import ReactDOM from 'react-dom/client';
import{
BrowserRouter as Router,
Routes,
Route,
Link,
Navigate
} from 'react-router-dom';
import Home from './Home';
import About from './About';
import Contact from './Contact';
import Dashboard from './Dashboard';
import Profile from './Profile';
import Settings from './Settings';
import User from './User';
const NotFound =()=>{
return
YouTip