0
0
NextJSframework~20 mins

Server-side state passing in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Server-side State Master
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 Next.js server component?

Consider this Next.js server component that fetches data and passes it to a client component. What will be rendered on the page?

NextJS
import React from 'react';

async function fetchData() {
  return { message: 'Hello from server' };
}

export default async function Page() {
  const data = await fetchData();
  return <ClientComponent message={data.message} />;
}

function ClientComponent({ message }) {
  'use client';
  return <p>{message}</p>;
}
AError: ClientComponent cannot be used in server component
B<p>undefined</p>
C<p>Loading...</p>
D<p>Hello from server</p>
Attempts:
2 left
💡 Hint

Remember that server components can pass props to client components.

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

In Next.js 14, server actions can update server state. Given this code, what will be the value of count after increment() is called?

NextJS
import { useState } from 'react';

let count = 0;

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

export default function Counter() {
  const [value, setValue] = useState(0);
  async function handleClick() {
    const newCount = await increment();
    setValue(newCount);
  }
  return <button onClick={handleClick}>Count: {value}</button>;
}
Aundefined
B1
CNaN
D0
Attempts:
2 left
💡 Hint

Server actions can update server-side variables and return new values.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a server action in Next.js 14?

Server actions in Next.js 14 must be defined with a special directive. Which option shows the correct syntax?

Aexport async function saveData() { "use server"; /*...*/ }
B"use client"; export async function saveData() { /*...*/ }
C"use server"; export async function saveData() { /*...*/ }
Dexport function saveData() { 'use client'; /*...*/ }
Attempts:
2 left
💡 Hint

The directive must be the first statement in the file or function.

🔧 Debug
advanced
2:00remaining
Why does this server component fail to render?

Given this Next.js server component, why does it cause a runtime error?

NextJS
export default async function Page() {
  const data = await fetch('http://localhost:3000/api/data');
  const json = await data.json();
  return <p>{json.message}</p>;
}
Afetch is not available in server components without special setup
BYou cannot use async functions in server components
CThe component must return a string, not JSX
Djson.message is undefined because API returns no message
Attempts:
2 left
💡 Hint

Think about how data fetching works in Next.js server components.

🧠 Conceptual
expert
2:00remaining
How does Next.js 14 handle state passing between server and client components?

Which statement best describes how Next.js 14 manages state passing between server and client components?

AServer components can pass serializable props to client components, but client components manage their own state locally
BClient components can directly modify server component state variables via hooks
CServer components and client components share the same state object automatically
DState is passed only through URL query parameters between server and client components
Attempts:
2 left
💡 Hint

Think about how data flows from server to client in React components.