Complete the code to define a route with a parameter named 'id'.
import { BrowserRouter, Routes, Route } from 'react-router-dom'; function App() { return ( <BrowserRouter> <Routes> <Route path="/user/[1]" element={<User />} /> </Routes> </BrowserRouter> ); }
In React Router, route parameters are defined with a colon before the parameter name, like :id.
Complete the code to access the 'id' parameter inside the User component using React Router hooks.
import { useParams } from 'react-router-dom'; function User() { const params = [1](); return <h1>User ID: {params.id}</h1>; }
The useParams hook returns an object of route parameters. Here, params.id accesses the 'id' parameter.
Fix the error in the code to correctly extract the 'postId' parameter from the URL.
function Post() {
const { [1] } = useParams();
return <div>Post ID: {postId}</div>;
}The parameter name must exactly match the route parameter name, which is 'postId' here.
Fill both blanks to create a route for 'productId' and access it inside the Product component.
import { useParams } from 'react-router-dom'; function Product() { const { [1] } = useParams(); return <p>Product: {productId}</p>; } function App() { return ( <BrowserRouter> <Routes> <Route path="/product/[2]" element={<Product />} /> </Routes> </BrowserRouter> ); }
The route parameter is named 'productId', so the path uses ':productId' and the component extracts 'productId' from useParams().
Fill all three blanks to create a route with two parameters and access them inside the Order component.
import { useParams } from 'react-router-dom'; function Order() { const { [1], [2] } = useParams(); return <div>Order {orderId} for user {userId}</div>; } function App() { return ( <BrowserRouter> <Routes> <Route path="/order/[3]/:userId" element={<Order />} /> </Routes> </BrowserRouter> ); }
The route has two parameters: 'orderId' and 'userId'. The path uses ':orderId' and ':userId'. The component extracts both from useParams().