3. Given this not-found.tsx component, what will be displayed when a user visits a missing page?
export default function NotFound() {
return (
<main role="main">
<h1>Oops! Page not found.</h1>
<p>Try going back or visit the homepage.</p>
<a href="/">Home</a>
</main>
);
}
medium
A. A page with a heading 'Oops! Page not found.', a paragraph, and a link to Home
B. A blank page with no content
C. An error message in the browser console only
D. A redirect to the homepage without showing any message
Solution
Step 1: Read the returned JSX
The component returns a main section with a heading, paragraph, and a link to the homepage.
Step 2: Understand what renders on missing page
When a page is missing, this component renders the content exactly as coded, showing the heading, paragraph, and link.
Final Answer:
A page with a heading 'Oops! Page not found.', a paragraph, and a link to Home -> Option A
Quick Check:
JSX content renders as visible page elements [OK]
Hint: JSX inside return shows on missing page [OK]
Common Mistakes:
Thinking it redirects automatically
Assuming no content shows
Confusing console errors with UI output
4. Identify the error in this not-found.tsx code snippet:
export default function NotFound() {
return (
<div>
<h1>Page Not Found</h1>
<a href="/">Go Home</a>
</div>
)
}
medium
A. Using <div> instead of <main> for accessibility
B. No error; code is valid and works correctly
C. Missing semicolon after return statement
D. Missing parentheses around JSX
Solution
Step 1: Check JSX syntax
The JSX is correctly wrapped and returned; no syntax errors or missing semicolons needed after return.
Step 2: Consider accessibility best practices
Using <main> instead of <div> improves accessibility by defining the main content region.
Final Answer:
Using <div> instead of <main> for accessibility -> Option A
Quick Check:
Use <main> for main content in error pages [OK]
Hint: Use <main> tag for main content, not just <div> [OK]
Common Mistakes:
Thinking semicolons are required after return
Ignoring accessibility tags
Assuming JSX needs extra parentheses
5. You want your not-found.tsx page to show a custom message only if the user is on a mobile device, otherwise show a default message. Which approach fits Next.js best?
hard
A. Create two separate not-found.tsx files, one for mobile and one for desktop
B. Use a React hook like useEffect with window.navigator.userAgent to detect device and conditionally render
C. Use CSS media queries to hide/show different messages in the same component
D. Write server-side code in not-found.tsx to detect device from request headers and render accordingly
Solution
Step 1: Understand client vs server rendering
Device detection using window.navigator.userAgent works only on client side, so use React hooks like useEffect.
Step 2: Evaluate other options
Server-side detection is complex and not typical in not-found.tsx. CSS media queries only style but don't change content. Multiple files for one route is invalid.
Final Answer:
Use a React hook like useEffect with window.navigator.userAgent to detect device and conditionally render -> Option B
Quick Check:
Client detection with hooks = best for device-specific UI [OK]
Hint: Detect device client-side with hooks, not server or multiple files [OK]