0
0
NextJSframework~10 mins

When to use client components in NextJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to mark a React component as a client component in Next.js.

NextJS
"use client";

export default function [1]() {
  return <button>Click me</button>;
}
Drag options to blanks, or click blank then click option'
AButtonStatic
BServerButton
CButtonServer
DClientButton
Attempts:
3 left
💡 Hint
Common Mistakes
Using a name that suggests server-side rendering instead of client-side.
Forgetting to add "use client" at the top of the file.
2fill in blank
medium

Complete the code to import a client component correctly in a Next.js server component.

NextJS
import [1] from './ClientButton';

export default function Page() {
  return <[1] />;
}
Drag options to blanks, or click blank then click option'
AServerButton
BButtonStatic
CClientButton
DStaticButton
Attempts:
3 left
💡 Hint
Common Mistakes
Importing a server component name instead of the client component.
Using a name that does not match the exported component.
3fill in blank
hard

Fix the error in the client component declaration by completing the missing directive.

NextJS
[1]

import React from 'react';

export default function Clicker() {
  const [count, setCount] = React.useState(0);
  return <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>;
}
Drag options to blanks, or click blank then click option'
A"use client";
B"use server";
Cimport React from 'react';
Dexport const client = true;
Attempts:
3 left
💡 Hint
Common Mistakes
Using "use server" instead of "use client".
Forgetting to add the directive at the top of the component.
4fill in blank
hard

Fill both blanks to correctly use a client component with state and event in Next.js.

NextJS
"use client";

import React, { [1] } from 'react';

export default function Counter() {
  const [count, setCount] = [2](0);
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
Drag options to blanks, or click blank then click option'
AuseState
BuseEffect
CuseRef
DuseContext
Attempts:
3 left
💡 Hint
Common Mistakes
Using hooks like useEffect or useRef instead of useState for state.
Importing the wrong hook or forgetting to import useState.
5fill in blank
hard

Fill all three blanks to create a client component that uses state and effect.

NextJS
"use client";

import React, { [1], [2] } from 'react';

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

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

  return <button>Seconds: {seconds}</button>;
}
Drag options to blanks, or click blank then click option'
AuseState
BuseEffect
CuseRef
DuseCallback
Attempts:
3 left
💡 Hint
Common Mistakes
Importing or using the wrong hooks like useRef or useCallback.
Not matching the hook usage with the import.