Lab 3
Lab 3
NAME: K BHAVANA
REG NO: 2200030550
lab:
Exercise 1:
Create a simple React application with two routes: Home and About.
Implement navigation links to switch between these routes using React Router.
Ans :
import React from 'react'; import { BrowserRouter as Router, Routes, Route,
Link } from 'react-router-dom'; import Home from './Home'; import About from
'./About';
return (
<Router>
<div>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
</ul>
</nav>
<Routes>
</Routes>
</div>
</Router>
);
};
Exercise 2: Implement a nested routing structure in a React application using React Router.
Create a parent route and two child routes that are rendered within the parent component
Ans :
import React from 'react'; import { BrowserRouter as Router, Routes,
return (
<Router>
<Routes>
</Route>
</Routes>
</Router>
);
};
} from 'react-router-dom';
return (
<div>
<Outlet />
</div>
);
};
outputs:
Post lab :
Question 1: How can you create a dynamic route in React Router that accepts a parameter and
renders different content based on that parameter?
Ans :
// ProductDetail.js import React from 'react';
dom';
return (
<div>
<h2>Product Details</h2>
</div>
);
};
dom';
const Product = () => { let { id } = useParams(); // Access the
return (
<div>
<h2>Product Details</h2>
</div>
);
};
Question 2: How can you implement nested routes in React Router to create
a hierarchical navigation structure Ans :
import React from 'react'; import { BrowserRouter as Router, Routes,
return (
<Router>
<Routes>
<Route path="/products">
</Route>
</Routes>
</Router>
);
};
from 'react';
return (
<div>
</div>
);
};
= () => { return (
<div>
</div>
);
};
// ProductDetails.js import
return (
<div>
</div>
);
};