0
0
NextJSframework~20 mins

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

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