Next.js Course
Next.js
/
Intermediate

Debugging Questions

Definition

Questions that test your ability to diagnose and fix common, frustrating errors unique to the Next.js environment.

Explain Like I'm New

Next.js throws very specific errors that confuse standard React developers (like Hydration errors). Interviewers want to know that you've seen these errors before and know exactly how to fix them.

Real World Example

Being asked: 'Why am I getting a Hydration Mismatch error, and how do I fix it?'

Common Use Cases

  • Practical coding interviews
  • Pair programming interviews

Interactive Example

/* 
  Debugging Scenario: Fixing 'window is not defined'
  
  ❌ BROKEN CODE:
  export default function ThemeToggle() {
    const theme = window.localStorage.getItem('theme'); // CRASHES SERVER!
    return <div>{theme}</div>;
  }

  ✅ FIXED CODE:
  'use client';
  import { useState, useEffect } from 'react';

  export default function ThemeToggle() {
    const [theme, setTheme] = useState(null);
    
    // useEffect ONLY runs on the browser, safely after the server render!
    useEffect(() => {
      setTheme(window.localStorage.getItem('theme'));
    }, []);

    // Prevent hydration mismatch by returning nothing until client is ready
    if (!theme) return null; 
    
    return <div>{theme}</div>;
  }
*/

Interview Questions

basic

  • What causes a 'Hydration Mismatch' error in Next.js?

intermediate

  • You get the error: `window is not defined`. Why did this happen and how do you fix it?

Flash Cards

Question

Hydration Mismatch?

Click to reveal answer
Answer

It happens when the HTML generated by the Server is slightly different than the HTML generated by the Client during the first render. Common causes: Using `Math.random()`, using `new Date()`, or improperly formatted HTML tags (like putting a `<div>` inside a `<p>`).

Question

Window not defined?

Click to reveal answer
Answer

You tried to access browser APIs (`window`, `document`, `localStorage`) inside a Server Component (which runs in Node.js, where `window` does not exist). Fix it by moving that logic into a Client Component, and ensuring it only runs inside a `useEffect` hook (which only fires on the browser).