0
0
NextJSframework~20 mins

Client component boundaries in NextJS - Practice Problems & Coding Challenges

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