0
0
NextJSframework~20 mins

Use server directive in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Server Directive Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this server component?

Consider this Next.js server component using the use server directive:

"use server";

export default async function Greeting() {
  const name = await fetchName();
  return <h1>Hello, {name}!</h1>;
}

async function fetchName() {
  return "Alice";
}

What will this component render on the page?

NextJS
"use server";

export default async function Greeting() {
  const name = await fetchName();
  return <h1>Hello, {name}!</h1>;
}

async function fetchName() {
  return "Alice";
}
A<h1>Hello, undefined!</h1>
B<h1>Hello, Alice!</h1>
CError: fetchName is not defined
D<h1>Hello, {name}!</h1>
Attempts:
2 left
💡 Hint

Remember that server components can await async functions and render their results directly.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses the use server directive?

Which of the following Next.js component snippets correctly applies the use server directive?

A
export default function MyComponent() {
  return &lt;p&gt;Server component&lt;/p&gt;;
}
"use server";
B
export default function MyComponent() {
  "use server";
  return &lt;p&gt;Server component&lt;/p&gt;;
}
C
export default function MyComponent() {
  return &lt;p&gt;Server component&lt;/p&gt;;
}
// use server
D
"use server";

export default function MyComponent() {
  return &lt;p&gt;Server component&lt;/p&gt;;
}
Attempts:
2 left
💡 Hint

The use server directive must be the first statement in the file or component.

state_output
advanced
2:00remaining
What is the value of count after this server action?

Given this Next.js server component with a server action:

"use server";

import { useState } from 'react';

let count = 0;

export default function Counter() {
  async function increment() {
    count += 1;
  }

  return (
    <button onClick={increment}>Clicked {count} times</button>
  );
}

What will be the value of count after clicking the button 3 times?

NextJS
"use server";

import { useState } from 'react';

let count = 0;

export default function Counter() {
  async function increment() {
    count += 1;
  }

  return (
    <button onClick={increment}>Clicked {count} times</button>
  );
}
A0
BNaN
CError: count is not reactive
D3
Attempts:
2 left
💡 Hint

Remember server components do not keep state between renders on the client.

🔧 Debug
advanced
2:00remaining
Why does this server action cause a runtime error?

Examine this Next.js server component:

"use server";

export default function Form() {
  async function submit(data) {
    await saveData(data);
  }

  return (
    <form action={submit}>
      <input name="text" />
      <button type="submit">Send</button>
    </form>
  );
}

async function saveData(data) {
  console.log(data.text.toUpperCase());
}

When submitting the form, a runtime error occurs. What is the cause?

NextJS
"use server";

export default function Form() {
  async function submit(data) {
    await saveData(data);
  }

  return (
    <form action={submit}>
      <input name="text" />
      <button type="submit">Send</button>
    </form>
  );
}

async function saveData(data) {
  console.log(data.text.toUpperCase());
}
Asubmit function is missing 'use server' directive
BsaveData is not async
Cdata.text is undefined because form data is a FormData object, not a plain object
Dinput element lacks a value attribute
Attempts:
2 left
💡 Hint

Check the type of data received by the server action.

🧠 Conceptual
expert
2:00remaining
Which statement about the use server directive is TRUE?

Choose the correct statement about the use server directive in Next.js:

AIt marks a component or function to run only on the server and allows using server-only APIs.
BIt enables client-side state management inside server components.
CIt automatically converts client components to server components at build time.
DIt allows server components to directly access browser APIs like <code>window</code>.
Attempts:
2 left
💡 Hint

Think about where server components run and what APIs they can access.