0
0
NextJSframework~20 mins

Use client directive in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Client Directive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this Next.js component with 'use client'?

Consider this Next.js component that uses the use client directive. What will it display when rendered?

NextJS
"use client";
import { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);
  return (
    <button onClick={() => setCount(count + 1)}>
      Count: {count}
    </button>
  );
}
AThe component fails to render because 'use client' is missing.
BA button showing 'Count: 0' initially and increments count on each click.
CA runtime error because 'useState' cannot be used with 'use client'.
DA static text 'Count: 0' that does not change on click.
Attempts:
2 left
💡 Hint

Think about what use client enables in Next.js components.

📝 Syntax
intermediate
2:00remaining
Which option correctly applies the 'use client' directive in a Next.js component?

Choose the correct way to apply the use client directive in a Next.js component file.

Aexport default function MyComponent() {\n "use client";\n return <div>Hello</div>;\n}
Bimport React from 'react';\n"use client";\nexport default function MyComponent() { return <div>Hello</div>; }
C"use client";\nimport React from 'react';\nexport default function MyComponent() { return <div>Hello</div>; }
Dimport React from 'react';\nexport default function MyComponent() { return <div>"use client"; Hello</div>; }
Attempts:
2 left
💡 Hint

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

🔧 Debug
advanced
2:00remaining
Why does this Next.js component fail to update state without 'use client'?

Given this component code, why does the count not update when clicking the button?

NextJS
import { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);
  return (
    <button onClick={() => setCount(count + 1)}>
      Count: {count}
    </button>
  );
}
ABecause the component is rendered as a server component by default, so hooks like useState do not work.
BBecause useState is not imported correctly from React.
CBecause the button element does not support onClick events.
DBecause the count variable is declared as a constant and cannot be updated.
Attempts:
2 left
💡 Hint

Think about the default rendering behavior of Next.js components without use client.

🧠 Conceptual
advanced
2:00remaining
What is the main purpose of the 'use client' directive in Next.js?

Choose the best explanation for why Next.js uses the use client directive.

AIt optimizes server-side rendering by caching component output.
BIt disables React's strict mode for faster rendering.
CIt automatically converts JavaScript code to TypeScript.
DIt marks a component to be rendered on the client side, enabling React hooks and browser APIs.
Attempts:
2 left
💡 Hint

Consider what features require client-side rendering in React.

state_output
expert
2:00remaining
What is the value of 'count' after clicking the button twice in this Next.js client component?

Given this component, what will be the value of count after clicking the button two times?

NextJS
"use client";
import { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);

  function handleClick() {
    setCount(count + 1);
    setCount(count + 1);
  }

  return (
    <button onClick={handleClick}>
      Count: {count}
    </button>
  );
}
ACount will be 2 after two clicks.
BCount will be 0 because state updates are ignored.
CCount will be 1 after two clicks.
DCount will be 3 after two clicks.
Attempts:
2 left
💡 Hint

Remember how React batches state updates and how closures capture state values.