Bird
Raised Fist0
NextJSframework~20 mins

Client component boundaries in NextJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Client Component Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a Server Component tries to use useState?
Consider a Next.js Server Component that includes the following code snippet:
import { useState } from 'react';

export default function ServerComp() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}

What will be the result when this component is rendered?
NextJS
import { useState } from 'react';

export default function ServerComp() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
AThe component renders correctly and the button updates the count on click.
BThe component renders with count always 0 and no errors.
CThe component renders but the button click does not update the count.
DThe component throws a runtime error because useState cannot be used in Server Components.
Attempts:
2 left
💡 Hint
Remember that useState is a React hook meant for client-side interactivity.
📝 Syntax
intermediate
1:30remaining
Identify the correct way to mark a Client Component in Next.js
Which of the following code snippets correctly marks a React component as a Client Component in Next.js?
Aexport default function MyComponent() { return <div>Hello</div>; }\n"use client";
B"use client";\nexport default function MyComponent() { return <div>Hello</div>; }
Cexport default function MyComponent() { return <div>Hello</div>; } // use client
D/* use client */\nexport default function MyComponent() { return <div>Hello</div>; }
Attempts:
2 left
💡 Hint
The 'use client' directive must be the very first line in the file.
❓ state_output
advanced
2:30remaining
What is the output of nested Client and Server Components with state?
Given the following Next.js components:
// ServerComp.jsx
export default function ServerComp() {
  return ;
}

// ClientComp.jsx
'use client';
import { useState } from 'react';
export default function ClientComp() {
  const [count, setCount] = useState(0);
  return ;
}

What will the user see and experience when ServerComp is rendered?
NextJS
// ServerComp.jsx
export default function ServerComp() {
  return <ClientComp />;
}

// ClientComp.jsx
'use client';
import { useState } from 'react';
export default function ClientComp() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
AThe button displays 'Count: 0' and increments the count on each click.
BThe button displays 'Count: 0' but clicking does not change the count.
CThe component throws an error because ServerComp cannot render ClientComp.
DThe button never renders because ClientComp is ignored inside ServerComp.
Attempts:
2 left
💡 Hint
Client Components can be nested inside Server Components to enable interactivity.
🔧 Debug
advanced
2:30remaining
Why does this Client Component fail to hydrate properly?
You have this Client Component in Next.js:
'use client';
import { useState, useEffect } from 'react';

export default function Timer() {
  const [seconds, setSeconds] = useState(0);

  useEffect(() => {
    const id = setInterval(() => setSeconds(s => s + 1), 1000);
    return () => clearInterval(id);
  }, []);

  return 
Seconds: {seconds}
; }

When rendered inside a Server Component, the timer does not update and the console shows a hydration mismatch warning. What is the most likely cause?
NextJS
'use client';
import { useState, useEffect } from 'react';

export default function Timer() {
  const [seconds, setSeconds] = useState(0);

  useEffect(() => {
    const id = setInterval(() => setSeconds(s => s + 1), 1000);
    return () => clearInterval(id);
  }, []);

  return <div>Seconds: {seconds}</div>;
}
AThe setInterval callback is not cleared properly, causing infinite re-renders.
BThe useEffect hook is not allowed in Client Components, causing hydration errors.
CThe Server Component renders the Timer with initial seconds 0, but the client re-renders with a different initial state causing mismatch.
DThe Timer component is missing a 'use server' directive, so hydration fails.
Attempts:
2 left
💡 Hint
Hydration mismatch often happens when server and client render different initial outputs.
🧠 Conceptual
expert
2:00remaining
Which statement best describes Client Component boundaries in Next.js?
Select the statement that correctly explains how Client Components and Server Components interact in Next.js 14+.
AServer Components can render Client Components, but Client Components cannot render Server Components inside them.
BClient Components and Server Components can be freely nested in any order without restrictions.
CServer Components and Client Components must be in separate folders and cannot be imported across boundaries.
DClient Components can only be rendered inside other Client Components; Server Components cannot include Client Components.
Attempts:
2 left
💡 Hint
Think about which component type can include the other.

Practice

(1/5)
1. What is the purpose of marking a component with "use client"; in Next.js?
easy
A. To optimize the component for server-side rendering.
B. To make the component run only on the server.
C. To disable the component from rendering.
D. To tell Next.js this component should run in the browser.

Solution

  1. Step 1: Understand client component marking

    Adding "use client"; at the top of a file tells Next.js this component runs in the browser, enabling interactivity.
  2. Step 2: Differentiate from server components

    Server components run on the server and do not have this directive. Marking a component as client enables browser-only features.
  3. Final Answer:

    To tell Next.js this component should run in the browser. -> Option D
  4. Quick Check:

    Client components = "use client" [OK]
Hint: Look for "use client" to identify browser-run components [OK]
Common Mistakes:
  • Thinking "use client" makes component server-only
  • Confusing client components with disabling rendering
  • Assuming it optimizes server rendering
2. Which of the following is the correct way to mark a React component as a client component in Next.js?
easy
A. Add "use client"; at the top of the component file.
B. Wrap the component in <Client></Client> tags.
C. Add client: true in the component props.
D. Import the component from a special client folder.

Solution

  1. Step 1: Recall client component syntax

    Next.js requires "use client"; at the very top of the file to mark it as a client component.
  2. Step 2: Evaluate other options

    Wrapping in tags, adding props, or importing from folders are not valid Next.js client component patterns.
  3. Final Answer:

    Add "use client"; at the top of the component file. -> Option A
  4. Quick Check:

    Client component syntax = "use client" [OK]
Hint: Always place "use client" as the first line in the file [OK]
Common Mistakes:
  • Trying to mark client components via props or wrappers
  • Placing "use client" inside the component function
  • Assuming folder structure controls client behavior
3. Given this Next.js component file content:
"use client";
import { useState } from 'react';

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

What will happen when this component is rendered in a Next.js app?
medium
A. The component will throw an error because useState is not allowed.
B. The button will display and update the count on clicks in the browser.
C. The button will render but clicks will not update the count.
D. The component will only render on the server and not be interactive.

Solution

  1. Step 1: Identify client component usage

    The file starts with "use client" so it runs in the browser and can use hooks like useState.
  2. Step 2: Understand interactivity

    The button uses onClick to update state, so clicks will increase the count and re-render the button text.
  3. Final Answer:

    The button will display and update the count on clicks in the browser. -> Option B
  4. Quick Check:

    Client component + useState = interactive button [OK]
Hint: Client components can use useState and handle events [OK]
Common Mistakes:
  • Thinking useState is disallowed in client components
  • Assuming server components handle clicks
  • Believing the button won't update without extra code
4. Consider this Next.js component file:
import { useState } from 'react';

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

What is the problem with this component regarding client boundaries?
medium
A. It lacks the "use client"; directive, so useState will cause an error.
B. It uses useState incorrectly inside a server component.
C. The onClick handler syntax is invalid in Next.js.
D. The component should be wrapped in a <Client> tag.

Solution

  1. Step 1: Check for client directive

    The component uses useState, which requires the component to be a client component marked by "use client".
  2. Step 2: Identify missing directive effect

    Without "use client", Next.js treats this as a server component, causing an error when using useState.
  3. Final Answer:

    It lacks the "use client"; directive, so useState will cause an error. -> Option A
  4. Quick Check:

    useState needs "use client" [OK]
Hint: useState requires "use client" at file top [OK]
Common Mistakes:
  • Assuming useState works without "use client"
  • Thinking onClick syntax is invalid
  • Trying to wrap component in tags for client behavior
5. You have a Next.js app with a server component Profile that fetches user data and a client component EditButton that allows editing the profile. How should you correctly use these components together?
hard
A. Import Profile inside EditButton and mark both with "use client";.
B. Mark Profile with "use client"; and import EditButton without any directive.
C. Import EditButton inside Profile and mark EditButton with "use client"; at its top.
D. Wrap EditButton inside Profile without marking either as client.

Solution

  1. Step 1: Understand component boundaries

    Server components can import client components, but client components cannot import server components.
  2. Step 2: Apply correct marking

    The EditButton must have "use client" at the top to run in the browser, while Profile remains a server component.
  3. Step 3: Correct usage

    Import EditButton inside Profile and mark EditButton with "use client"; this keeps boundaries correct and enables interactivity.
  4. Final Answer:

    Import EditButton inside Profile and mark EditButton with "use client"; at its top. -> Option C
  5. Quick Check:

    Server imports client; client marked "use client" [OK]
Hint: Server components import client components marked "use client" [OK]
Common Mistakes:
  • Marking server components as client unnecessarily
  • Trying to import server components inside client components
  • Not marking client components with "use client"
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