Complete the code to create a dynamic route file for a user profile page.
pages/[1].jsIn Next.js, dynamic route segments are defined by square brackets around the segment name, like [userId].
Complete the React component to access the dynamic route parameter using Next.js hooks.
import { useRouter } from 'next/router'; export default function Profile() { const router = useRouter(); const [1] = router.query.userId; return <p>User ID: {userId}</p>; }
The dynamic route parameter is accessed from router.query using the exact segment name, here userId.
Fix the error in the dynamic route component to correctly handle the case when the router is not ready.
import { useRouter } from 'next/router'; export default function Post() { const router = useRouter(); if (!router.isReady) return <p>Loading...</p>; const postId = router.query.[1]; return <p>Post ID: {postId}</p>; }
The dynamic segment name must match the file name. If the file is [postId].js, then use router.query.postId.
Fill all three blanks to create a dynamic route segment and access its parameter in the component.
import { useRouter } from 'next/router'; export default function Product() { const router = useRouter(); const [1] = router.query.[2]; return <p>Product ID: {productId}</p>; } // File path: pages/[3].js
The dynamic segment and the query parameter must have the same name, here productId.
Fill all three blanks to create a nested dynamic route and access both parameters in the component.
import { useRouter } from 'next/router'; export default function Review() { const router = useRouter(); const [1] = router.query.[2]; const [3] = router.query.reviewId; return <p>Product: {productId}, Review: {reviewId}</p>; } // File path: pages/products/[productId]/reviews/[reviewId].js
The nested dynamic route uses [productId] and [reviewId]. Access both from router.query with matching names.