0
0
NextJSframework~20 mins

Server and client component composition in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Server & Client 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 component render?

Consider this Next.js component structure using server and client components. What will be the rendered output in the browser?

NextJS
import ClientButton from './ClientButton';

export default function ServerComponent() {
  return (
    <main>
      <h1>Welcome</h1>
      <ClientButton />
    </main>
  );
}

// ClientButton.jsx
'use client';
import { useState } from 'react';

export default function ClientButton() {
  const [count, setCount] = useState(0);
  return (
    <button onClick={() => setCount(count + 1)}>
      Clicked {count} times
    </button>
  );
}
A<main><h1>Welcome</h1><button>Clicked 0 times</button></main>
B<main><h1>Welcome</h1><button>Clicked undefined times</button></main>
CError: Client components cannot be rendered inside server components
D<main><h1>Welcome</h1></main>
Attempts:
2 left
💡 Hint

Remember that client components can be used inside server components in Next.js 14+.

lifecycle
intermediate
2:00remaining
When does the client component's state initialize in this Next.js setup?

Given a server component rendering a client component with useState, when does the client component's state initialize?

NextJS
export default function ServerComp() {
  return <ClientComp />;
}

// ClientComp.jsx
'use client';
import { useState } from 'react';

export default function ClientComp() {
  const [value, setValue] = useState(5);
  return <div>{value}</div>;
}
ADuring server rendering on the server
BWhen the client component is hydrated in the browser
CImmediately when the server component renders
DOnly after a user interaction triggers state initialization
Attempts:
2 left
💡 Hint

Think about where React hooks like useState run in Next.js server/client components.

📝 Syntax
advanced
2:00remaining
Which option correctly marks a client component in Next.js 14+?

Choose the correct way to declare a client component in Next.js 14+.

A'use client';\nconst MyComponent = () => <div>Hello</div>; export default MyComponent;
Bexport default function MyComponent() { 'use client'; return <div>Hello</div>; }
Cexport default function MyComponent() { return <div>'use client'; Hello</div>; }
D'use client';\nexport default function MyComponent() { return <div>Hello</div>; }
Attempts:
2 left
💡 Hint

The 'use client' directive must be the first line in the file.

🔧 Debug
advanced
2:00remaining
Why does this Next.js app crash with 'useState is not defined' error?

Examine the client component code below. Why does it crash with 'useState is not defined'?

NextJS
'use client';

export default function Button() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Clicked {count}</button>;
}
AuseState is not imported from 'react'
BThe component must be a class component to use useState
CuseState cannot be used in client components
DThe 'use client' directive is missing
Attempts:
2 left
💡 Hint

Check if all React hooks are properly imported.

🧠 Conceptual
expert
2:00remaining
What is the main reason to split components into server and client in Next.js 14+?

Why does Next.js 14+ encourage separating components into server and client types?

ATo prevent any JavaScript from running on the client
BTo force all components to use React hooks
CTo optimize performance by rendering static parts on the server and interactive parts on the client
DTo make all components render only on the client for faster updates
Attempts:
2 left
💡 Hint

Think about how server and client components affect loading speed and interactivity.