Bird
Raised Fist0
NextJSframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of getServerSideProps in Next.js?
easy
A. To manage browser cookies
B. To handle client-side routing
C. To style components dynamically
D. To fetch data on the server before rendering the page

Solution

  1. Step 1: Understand getServerSideProps role

    This function runs on the server before the page loads to fetch or prepare data.
  2. Step 2: Identify its main use

    It ensures the page has fresh data on first load and improves SEO by server-side rendering.
  3. Final Answer:

    To fetch data on the server before rendering the page -> Option D
  4. Quick Check:

    Server-side data fetching = A [OK]
Hint: Remember: server-side means before page shows [OK]
Common Mistakes:
  • Confusing client-side routing with server-side data fetching
  • Thinking it styles components
  • Assuming it manages cookies directly
2. Which of the following is the correct syntax to export getServerSideProps in a Next.js page?
easy
A. export default getServerSideProps = () => ({ props: {} })
B. export async function getServerSideProps(context) { return { props: {} } }
C. function getServerSideProps() { return props: {} }
D. export getServerSideProps = async () => { return {} }

Solution

  1. Step 1: Recall correct export syntax

    getServerSideProps must be an async function exported with the exact name.
  2. Step 2: Check return structure

    It returns an object with a props key containing data for the page.
  3. Final Answer:

    export async function getServerSideProps(context) { return { props: {} } } -> Option B
  4. Quick Check:

    Correct async export with props = D [OK]
Hint: Use async function with named export and return props [OK]
Common Mistakes:
  • Using arrow function without export keyword
  • Returning object without wrapping props
  • Using default export incorrectly
3. Given this code snippet in a Next.js page, what will be the rendered output?
export async function getServerSideProps() {
  return { props: { message: 'Hello from server' } };
}

export default function Page({ message }) {
  return <h1>{message}</h1>;
}
medium
A. <h1>Hello from server</h1>
B. Error: message is undefined
C. <h1>{message}</h1>
D. <h1>Hello from client</h1>

Solution

  1. Step 1: Understand data flow

    getServerSideProps passes message as a prop to the page component.
  2. Step 2: Render output

    The component receives message and renders it inside an <h1> tag.
  3. Final Answer:

    <h1>Hello from server</h1> -> Option A
  4. Quick Check:

    Server prop passed and rendered = A [OK]
Hint: Props from server appear as component parameters [OK]
Common Mistakes:
  • Thinking props are undefined without client fetch
  • Rendering literal {message} instead of value
  • Confusing server message with client message
4. What is wrong with this getServerSideProps function?
export async function getServerSideProps() {
  const data = await fetch('https://api.example.com/data');
  return data;
}
medium
A. The returned props must be an object with a props key wrapping data
B. The fetch call is missing await
C. getServerSideProps cannot be async
D. The function should return data directly, not inside props

Solution

  1. Step 1: Check fetch usage

    The fetch is awaited correctly, so no issue there.
  2. Step 2: Check return structure

    The function must return an object with a props key containing an object. Here, data is returned directly, which is incorrect.
  3. Final Answer:

    The returned props must be an object with a props key wrapping data -> Option A
  4. Quick Check:

    Return { props: { ... } } not just data [OK]
Hint: Always wrap data inside props object in return [OK]
Common Mistakes:
  • Returning data directly instead of inside props
  • Forgetting to await fetch
  • Thinking async is not allowed
5. You want to pass user data fetched from a database to a Next.js page using getServerSideProps. Which approach correctly handles the asynchronous fetch and passes the data safely to the page component?
hard
A. export async function getServerSideProps() { const user = getUserFromDB(); return { props: user }; }
B. export function getServerSideProps() { const user = getUserFromDB(); return { props: { user } }; }
C. export async function getServerSideProps() { const user = await getUserFromDB(); return { props: { user } }; }
D. export async function getServerSideProps() { const user = await getUserFromDB(); return user; }

Solution

  1. Step 1: Handle async data fetching

    The function must be async and await the database call to get user data.
  2. Step 2: Return data wrapped in props object

    The returned object must have a props key containing an object with user data.
  3. Final Answer:

    export async function getServerSideProps() { const user = await getUserFromDB(); return { props: { user } }; } -> Option C
  4. Quick Check:

    Async await + return props object = C [OK]
Hint: Await async fetch and wrap result inside props [OK]
Common Mistakes:
  • Not awaiting async database call
  • Returning data directly without props wrapper
  • Using non-async function for async fetch
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard