Bird
Raised Fist0
NextJSframework~20 mins

Client-only modules in NextJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Client-Only Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What will this Next.js client-only component render?

Consider this Next.js component that uses a client-only module:

"use client";
import dynamic from 'next/dynamic';

const ClientOnlyComponent = dynamic(() => import('./HeavyClientComponent'), { ssr: false });

export default function Page() {
  return (
    <div>
      <h1>Welcome</h1>
      <ClientOnlyComponent />
    </div>
  );
}

What will the user see when this page loads?

NextJS
"use client";
import dynamic from 'next/dynamic';

const ClientOnlyComponent = dynamic(() => import('./HeavyClientComponent'), { ssr: false });

export default function Page() {
  return (
    <div>
      <h1>Welcome</h1>
      <ClientOnlyComponent />
    </div>
  );
}
AThe page shows "Welcome" but never loads HeavyClientComponent because ssr is false.
BThe page shows "Welcome" immediately, and the HeavyClientComponent loads only on the client side after hydration.
CThe page waits to load until HeavyClientComponent is server-rendered and sent fully rendered to the client.
DThe page shows an error because dynamic imports cannot be used with client-only modules.
Attempts:
2 left
💡 Hint

Think about what setting ssr: false does for dynamic imports in Next.js.

📝 Syntax
intermediate
1:30remaining
Which import statement correctly marks a Next.js module as client-only?

You want to create a client-only React component in Next.js. Which of these import statements correctly marks the module as client-only?

A"use client";\nimport React from 'react';
Bimport React from 'react';\n"use client";
Cimport React from 'react'; // client-only
Dimport React from 'react';\nexport const client = true;
Attempts:
2 left
💡 Hint

Remember where the "use client"; directive must be placed in a module.

🔧 Debug
advanced
2:30remaining
Why does this client-only module cause a hydration mismatch error?

Given this Next.js page:

"use client";
import { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);
  if (typeof window === 'undefined') {
    return <div>Loading...</div>;
  }
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}

Why does this cause a hydration mismatch error?

NextJS
"use client";
import { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);
  if (typeof window === 'undefined') {
    return <div>Loading...</div>;
  }
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
ABecause the component returns different content on server and client, causing React to detect a mismatch during hydration.
BBecause useState cannot be used in client components, only server components.
CBecause the "use client" directive is missing at the top of the file.
DBecause the button element is missing an aria-label for accessibility.
Attempts:
2 left
💡 Hint

Think about what React expects when hydrating server-rendered content on the client.

❓ state_output
advanced
1:30remaining
What is the value of count after clicking the button twice in this client-only component?

Look at this Next.js client-only component:

"use client";
import { useState } from 'react';

export default function Clicker() {
  const [count, setCount] = useState(0);
  return (
    <button onClick={() => setCount(prev => prev + 1)}>
      Clicked {count} times
    </button>
  );
}

If the user clicks the button twice, what will be the displayed count?

NextJS
"use client";
import { useState } from 'react';

export default function Clicker() {
  const [count, setCount] = useState(0);
  return (
    <button onClick={() => setCount(prev => prev + 1)}>
      Clicked {count} times
    </button>
  );
}
AClicked NaN times
BClicked 1 times
CClicked 0 times
DClicked 2 times
Attempts:
2 left
💡 Hint

Remember how the updater function form of setState works in React.

🧠 Conceptual
expert
2:30remaining
Which statement about Next.js client-only modules is TRUE?

Choose the correct statement about client-only modules in Next.js:

AClient-only modules can import server-only APIs like database clients without errors because they run only on the client.
BClient-only modules must always be dynamically imported with <code>ssr: false</code> to avoid hydration errors.
CClient-only modules can use React hooks like useState and useEffect, but cannot use server-side data fetching methods.
DMarking a module with "use client" disables server-side rendering for that module and all its children components.
Attempts:
2 left
💡 Hint

Think about what code runs on the client vs server in Next.js and what hooks are allowed.

Practice

(1/5)
1. What is the main reason to use client-only modules in Next.js?
easy
A. They run only in the browser, avoiding server errors with browser APIs.
B. They improve server-side rendering speed by running on the server.
C. They allow using Node.js modules on the server.
D. They enable static site generation without JavaScript.

Solution

  1. Step 1: Understand client-only module purpose

    Client-only modules run only in the browser to avoid errors from browser-specific APIs on the server.
  2. Step 2: Compare options

    They run only in the browser, avoiding server errors with browser APIs. correctly states this purpose. Options B, C, and D describe unrelated or incorrect behaviors.
  3. Final Answer:

    They run only in the browser, avoiding server errors with browser APIs. -> Option A
  4. Quick Check:

    Client-only modules = browser-only code [OK]
Hint: Client-only means browser-only to avoid server API errors [OK]
Common Mistakes:
  • Thinking client-only modules run on the server
  • Confusing client-only with static generation
  • Assuming client-only modules improve server speed
2. Which syntax correctly imports a client-only module named MyComponent using Next.js dynamic import?
easy
A. const MyComponent = require('./MyComponent');
B. import dynamic from 'next/dynamic'; const MyComponent = dynamic(() => import('./MyComponent'), { ssr: false });
C. import MyComponent from './MyComponent';
D. import dynamic from 'next/dynamic'; const MyComponent = dynamic(() => import('./MyComponent'));

Solution

  1. Step 1: Recall dynamic import with ssr: false

    To load client-only modules, Next.js requires dynamic import with { ssr: false } to disable server-side rendering.
  2. Step 2: Analyze options

    import dynamic from 'next/dynamic'; const MyComponent = dynamic(() => import('./MyComponent'), { ssr: false }); uses dynamic import with ssr: false correctly. import MyComponent from './MyComponent'; is a normal import (runs on server too). const MyComponent = require('./MyComponent'); uses require, not recommended in Next.js. import dynamic from 'next/dynamic'; const MyComponent = dynamic(() => import('./MyComponent')); misses ssr: false, so it runs on server.
  3. Final Answer:

    import dynamic from 'next/dynamic'; const MyComponent = dynamic(() => import('./MyComponent'), { ssr: false }); -> Option B
  4. Quick Check:

    Use dynamic + ssr: false for client-only imports [OK]
Hint: Use dynamic import with ssr: false for client-only modules [OK]
Common Mistakes:
  • Forgetting { ssr: false } disables server rendering
  • Using normal import for client-only modules
  • Using require instead of import
3. Given this code snippet in Next.js, what will be the rendered output?
import dynamic from 'next/dynamic';
const ClientOnly = dynamic(() => import('./ClientOnlyComponent'), { ssr: false });

export default function Page() {
  return (
    

Welcome

); }

Assuming ClientOnlyComponent renders <p>Loaded on client</p>.
medium
A.

Welcome

Loaded on client

B.

Welcome

C.

Welcome

Loading...

D. Error: ClientOnlyComponent cannot be rendered on server

Solution

  1. Step 1: Understand dynamic import with ssr: false

    The component ClientOnly loads only on the client, so server renders without it initially, showing a loading placeholder.
  2. Step 2: Predict final render output

    Before hydration, the placeholder Loading... is shown. After hydration, ClientOnlyComponent renders <p>Loaded on client</p>. The initial render output includes the heading and loading placeholder.
  3. Final Answer:

    <div><h1>Welcome</h1><p>Loading...</p></div> -> Option C
  4. Quick Check:

    Client-only modules show loading placeholder on server render [OK]
Hint: Client-only components show loading placeholder before hydration [OK]
Common Mistakes:
  • Expecting client-only content on server render
  • Assuming error occurs without server rendering
  • Thinking loading placeholder always shows
4. You wrote this code to import a client-only module:
import dynamic from 'next/dynamic';
const Widget = dynamic(() => import('./Widget'));

export default function Home() {
  return ;
}

What is the main problem here?
medium
A. Missing { ssr: false } causes server to try rendering Widget, which may fail.
B. Dynamic import syntax is incorrect; should use require instead.
C. Widget component must be imported normally, not dynamically.
D. You must wrap Widget in a Suspense component for dynamic imports.

Solution

  1. Step 1: Check dynamic import options

    The code uses dynamic import but omits { ssr: false }, so Next.js tries to render Widget on the server.
  2. Step 2: Understand consequences

    If Widget uses browser-only APIs, server rendering will fail or cause errors. Adding { ssr: false } prevents server rendering.
  3. Final Answer:

    Missing { ssr: false } causes server to try rendering Widget, which may fail. -> Option A
  4. Quick Check:

    Always add ssr: false for client-only modules [OK]
Hint: Add { ssr: false } to disable server rendering for client-only modules [OK]
Common Mistakes:
  • Forgetting ssr: false option
  • Thinking dynamic import syntax is wrong without require
  • Assuming Suspense is mandatory for dynamic imports
5. You want to use a browser-only API inside a Next.js component, but only on the client side. Which approach correctly ensures this without server errors?
import dynamic from 'next/dynamic';

const BrowserOnly = dynamic(() => import('./BrowserOnlyComponent'), { ssr: false });

export default function Page() {
  return (
    

Client-only Example

); }

What else should you do inside BrowserOnlyComponent to safely use browser APIs?
hard
A. Wrap browser API calls inside a try-catch in the component body.
B. Call browser APIs directly in the component body without hooks.
C. Use getServerSideProps to fetch browser API data on server.
D. Use React's useEffect hook to run browser API code after component mounts.

Solution

  1. Step 1: Understand client-only import

    Dynamic import with { ssr: false } ensures the component loads only on the client, avoiding server errors.
  2. Step 2: Use useEffect for browser APIs

    Inside the component, browser APIs should be called inside useEffect to run after mounting, ensuring the DOM and window objects exist.
  3. Final Answer:

    Use React's useEffect hook to run browser API code after component mounts. -> Option D
  4. Quick Check:

    Browser APIs inside useEffect = safe client use [OK]
Hint: Put browser API calls inside useEffect to run after mount [OK]
Common Mistakes:
  • Calling browser APIs directly in render causing errors
  • Trying to use browser APIs on server via getServerSideProps
  • Relying on try-catch instead of proper lifecycle hooks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard