0
0
NextJSframework~20 mins

When to use client components 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!
🧠 Conceptual
intermediate
2:00remaining
When should you use a client component in Next.js?
In Next.js, which scenario best requires using a client component instead of a server component?
AFetching data from a database on the server before rendering the page
BRendering static content that never changes and does not need user interaction
CHandling user input events like clicks or form submissions directly in the component
DGenerating SEO-friendly metadata for the page
Attempts:
2 left
💡 Hint
Think about when you need to respond to user actions immediately in the browser.
component_behavior
intermediate
2:00remaining
What happens if you use a server component to handle a button click event?
Consider a Next.js server component that tries to handle a button click event with an onClick handler. What will happen when the user clicks the button?
AThe button will not respond to clicks because server components do not support client-side events
BThe click event handler will cause a runtime error because server components cannot handle events
CThe click event will trigger a full page reload to handle the event on the server
DThe click event will be handled immediately in the browser as expected
Attempts:
2 left
💡 Hint
Remember where server components run and what they can do.
📝 Syntax
advanced
2:00remaining
Identify the correct way to mark a component as a client component in Next.js
Which of the following code snippets correctly marks a React component as a client component in Next.js 14+?
NextJS
import React from 'react';

// Component code here
Aexport default function MyComponent() { return <button>Click</button>; } // add client: true in next.config.js
Bexport const MyComponent = () => { return <button>Click</button>; } // no special directive needed
Cimport { client } from 'next/client';\n\nexport default client(function MyComponent() { return <button>Click</button>; });
D"use client";\n\nexport default function MyComponent() { return <button>Click</button>; }
Attempts:
2 left
💡 Hint
Look for the special directive that tells Next.js this is a client component.
state_output
advanced
2:00remaining
What is the output when a client component updates state on button click?
Given this Next.js client component code, what will be displayed after clicking the button twice?
NextJS
"use client";
import { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);
  return (
    <>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </>
  );
}
ACount: 2
BCount: 1
CCount: 0
DCount: undefined
Attempts:
2 left
💡 Hint
Each click increases the count by one starting from zero.
🔧 Debug
expert
2:00remaining
Why does this client component fail to update state on click?
This Next.js client component does not update the count when the button is clicked. What is the most likely cause?
NextJS
import { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);
  return (
    <>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </>
  );
}
AThe onClick handler syntax is incorrect and should be onClick={setCount(count + 1)}
BThe component is missing the "use client" directive at the top of the file
CuseState hook cannot be used inside functional components
DReact does not support updating state inside fragments <> </>
Attempts:
2 left
💡 Hint
Think about what tells Next.js this component runs on the client.