A client-only module is a part of your Next.js app that runs only in the browser, not on the server. It is used for code that depends on browser APIs or user interactions.
Click to reveal answer
beginner
How do you mark a module as client-only in Next.js 14+?
You add the directive 'use client' at the top of the module file. This tells Next.js to load and run this module only on the client side.
Click to reveal answer
intermediate
Why should you avoid using server-only modules inside client-only modules?
Server-only modules use Node.js or server APIs that don't exist in the browser. Using them in client-only modules causes errors because the browser can't run server code.
Click to reveal answer
intermediate
What happens if you forget to add 'use client' in a module that uses browser-only APIs?
Next.js will try to run the module on the server, causing errors like 'window is not defined' because browser APIs don't exist on the server.
Click to reveal answer
beginner
Give an example of when to use a client-only module in Next.js.
Use client-only modules for things like handling user input, animations, or accessing localStorage, which only work in the browser.
Click to reveal answer
What directive do you add to a Next.js module to make it client-only?
A'use client'
B'use server'
C'client-only'
D'browser-only'
✗ Incorrect
The correct directive is 'use client' placed at the top of the module.
Which of these is a reason to use a client-only module?
ATo run server-side authentication
BTo run database queries
CTo render static HTML
DTo access browser APIs like localStorage
✗ Incorrect
Browser APIs like localStorage only work in client-side code.
What error might you see if you use browser APIs in a server module?
A'database connection failed'
B'module not found'
C'window is not defined'
D'syntax error'
✗ Incorrect
'window is not defined' happens because the server has no window object.
Where does a client-only module run in a Next.js app?
AIn the browser only
BOn the server only
CBoth server and browser
DIn the database
✗ Incorrect
Client-only modules run exclusively in the browser.
What happens if you forget 'use client' in a module that uses React hooks?
AIt runs fine without any issues
BNext.js treats it as a server component and errors may occur
CThe module is ignored
DThe app crashes immediately
✗ Incorrect
React hooks require client components; without 'use client', Next.js treats the module as server-side.
Explain what a client-only module is in Next.js and why you would use it.
Think about where the code runs and what it needs to access.
You got /4 concepts.
Describe the consequences of not adding 'use client' to a module that uses browser features.
What happens when server tries to run browser code?
You got /4 concepts.
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
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.
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.
Final Answer:
They run only in the browser, avoiding server errors with browser APIs. -> Option A
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
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.
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.
Final Answer:
import dynamic from 'next/dynamic'; const MyComponent = dynamic(() => import('./MyComponent'), { ssr: false }); -> Option B
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?
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
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.
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.
Final Answer:
<div><h1>Welcome</h1><p>Loading...</p></div> -> Option C
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
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.
Step 2: Understand consequences
If Widget uses browser-only APIs, server rendering will fail or cause errors. Adding { ssr: false } prevents server rendering.
Final Answer:
Missing { ssr: false } causes server to try rendering Widget, which may fail. -> Option A
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?