Bird
Raised Fist0
NextJSframework~20 mins

Not-found.tsx customization in NextJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Not-found.tsx Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this custom Not-found.tsx component render?

Consider this Next.js 14 custom not-found.tsx component:

export default function NotFound() {
  return (
    <main role="main" aria-label="Page not found">
      <h1>Oops! Page missing.</h1>
      <p>Sorry, we couldn't find what you were looking for.</p>
    </main>
  )
}

What will be the visible output when a user visits a non-existent page?

NextJS
export default function NotFound() {
  return (
    <main role="main" aria-label="Page not found">
      <h1>Oops! Page missing.</h1>
      <p>Sorry, we couldn't find what you were looking for.</p>
    </main>
  )
}
AA blank page with no content rendered.
BA page with heading 'Oops! Page missing.' and a paragraph apologizing for missing content.
CA default Next.js 404 page ignoring this component.
DAn error message saying 'Component NotFound not found'.
Attempts:
2 left
💡 Hint

Custom not-found.tsx replaces the default 404 page in Next.js.

📝 Syntax
intermediate
1:30remaining
Which option correctly exports a NotFound component in Next.js 14?

Choose the correct syntax to export a NotFound component in not-found.tsx for Next.js 14.

Aexport NotFound = () => <h1>Not Found</h1>
Bfunction NotFound() { return <h1>Not Found</h1> } export NotFound
Cexport default function NotFound() { return <h1>Not Found</h1> }
Dexport default NotFound = () => <h1>Not Found</h1>
Attempts:
2 left
💡 Hint

Remember to use export default function syntax for components.

state_output
advanced
2:30remaining
What happens if you use useState inside not-found.tsx?

Given this not-found.tsx component in Next.js 14:

import { useState } from 'react';

export default function NotFound() {
  const [count, setCount] = useState(0);
  return (
    

Page Not Found

) }

What will be the behavior when this page is shown?

NextJS
import { useState } from 'react';

export default function NotFound() {
  const [count, setCount] = useState(0);
  return (
    <main>
      <h1>Page Not Found</h1>
      <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>
    </main>
  )
}
AThe count will always stay 0 because the component is server rendered only.
BThe button will increment the count on each click, showing updated count.
CThe page will not render at all due to invalid hook usage.
DNext.js will throw a runtime error because useState is not allowed in not-found.tsx.
Attempts:
2 left
💡 Hint

Next.js 14 supports client components inside not-found.tsx if you use 'use client'.

🔧 Debug
advanced
2:00remaining
Why does this not-found.tsx cause a 500 error?

Review this not-found.tsx code:

export default function NotFound() {
  return (
    <div>
      <h1>Page Not Found</h1>
      <button onClick={() => alert('Oops!')}>Click me</button>
    </div>
  )
}

Visiting a missing page causes a server error 500. Why?

NextJS
export default function NotFound() {
  return (
    <div>
      <h1>Page Not Found</h1>
      <button onClick={() => alert('Oops!')}>Click me</button>
    </div>
  )
}
ABecause event handlers like onClick are not allowed in server components without 'use client'.
BBecause alert is not defined in Next.js environment.
CBecause the component must return a <main> element, not <div>.
DBecause the button tag is invalid in Next.js 14.
Attempts:
2 left
💡 Hint

Think about client vs server components and event handlers.

🧠 Conceptual
expert
3:00remaining
How does Next.js 14 decide to render not-found.tsx?

In Next.js 14, when does the framework render the not-found.tsx component?

Choose the most accurate explanation.

AWhen a route segment or dynamic route returns <code>notFound()</code> from a server component, Next.js renders <code>not-found.tsx</code> for that segment.
BNext.js always renders <code>not-found.tsx</code> on any 404 error regardless of route or data fetching.
CNext.js renders <code>not-found.tsx</code> only if the file is inside the <code>pages</code> directory.
DNext.js renders <code>not-found.tsx</code> only if the user manually navigates to /not-found URL.
Attempts:
2 left
💡 Hint

Think about how Next.js 14 handles dynamic routes and server components.

Practice

(1/5)
1. What is the main purpose of the not-found.tsx file in a Next.js app?
easy
A. To define the main layout of the website
B. To handle user authentication
C. To customize the page shown when a user visits a missing URL
D. To manage API routes

Solution

  1. Step 1: Understand the role of not-found.tsx

    This file is specifically for customizing the page shown when a user tries to access a page that does not exist.
  2. Step 2: Compare with other options

    Other options like layout, authentication, or API routes are handled by different files or folders in Next.js.
  3. Final Answer:

    To customize the page shown when a user visits a missing URL -> Option C
  4. Quick Check:

    Missing page display = Custom not-found.tsx [OK]
Hint: Remember: not-found.tsx shows missing page content [OK]
Common Mistakes:
  • Confusing not-found.tsx with layout or API files
  • Thinking it handles authentication
  • Assuming it manages site navigation
2. Which of the following is the correct way to export a default React component in not-found.tsx?
easy
A. default export function NotFound() { return

Page Not Found

; }
B. function NotFound() { return

Page Not Found

; } export NotFound;
C. export function NotFound() { return

Page Not Found

; }
D. export default function NotFound() { return

Page Not Found

; }

Solution

  1. Step 1: Recall correct default export syntax

    In React with Next.js, the correct way is to write export default function ComponentName() { ... }.
  2. Step 2: Check each option

    export default function NotFound() { return

    Page Not Found

    ; } matches the correct syntax. function NotFound() { return

    Page Not Found

    ; } export NotFound; has incorrect export syntax. export function NotFound() { return

    Page Not Found

    ; } exports a named function, not default. default export function NotFound() { return

    Page Not Found

    ; } uses invalid syntax.
  3. Final Answer:

    export default function NotFound() { return <h1>Page Not Found</h1>; } -> Option D
  4. Quick Check:

    Default export = export default function [OK]
Hint: Default export uses 'export default function' syntax [OK]
Common Mistakes:
  • Using named export instead of default
  • Incorrect export keyword placement
  • Missing 'default' keyword
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

  1. Step 1: Read the returned JSX

    The component returns a main section with a heading, paragraph, and a link to the homepage.
  2. 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.
  3. Final Answer:

    A page with a heading 'Oops! Page not found.', a paragraph, and a link to Home -> Option A
  4. 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

  1. Step 1: Check JSX syntax

    The JSX is correctly wrapped and returned; no syntax errors or missing semicolons needed after return.
  2. Step 2: Consider accessibility best practices

    Using <main> instead of <div> improves accessibility by defining the main content region.
  3. Final Answer:

    Using <div> instead of <main> for accessibility -> Option A
  4. 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

  1. Step 1: Understand client vs server rendering

    Device detection using window.navigator.userAgent works only on client side, so use React hooks like useEffect.
  2. 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.
  3. Final Answer:

    Use a React hook like useEffect with window.navigator.userAgent to detect device and conditionally render -> Option B
  4. 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]
Common Mistakes:
  • Trying to detect device server-side in this file
  • Using multiple not-found.tsx files
  • Relying only on CSS for content changes