0
0
NextJSframework~20 mins

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

Choose your learning style9 modes available
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.