0
0
NextJSframework~20 mins

Server state vs client state in NextJS - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Server and Client State Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Server State vs Client State in Next.js

In Next.js, which of the following best describes server state compared to client state?

AServer state is data fetched and rendered on the server before sending HTML to the client, while client state is data managed and updated within the browser after page load.
BServer state is data stored only in the browser's memory, while client state is data fetched from external APIs on the server.
CServer state and client state are the same; both refer to data stored in React components on the client side.
DServer state is data that never changes, while client state is data that changes only on the server.
Attempts:
2 left
💡 Hint

Think about where the data lives and when it is fetched or updated.

component_behavior
intermediate
2:00remaining
Next.js Component Behavior with Server and Client State

Consider a Next.js page that fetches a list of products on the server and also allows users to filter products on the client. Which statement best describes how server and client state work together here?

AFiltering updates the server state directly without any client-side changes.
BFiltering triggers a full server reload to update the product list each time the user changes the filter.
CThe product list is fetched on the server and passed as props; filtering happens in client state without refetching from the server.
DThe product list is fetched on the client only after the page loads; server state is not used.
Attempts:
2 left
💡 Hint

Think about how initial data is loaded and how user interactions update the UI.

📝 Syntax
advanced
2:00remaining
Identifying Server State Fetching in Next.js

Which of the following Next.js code snippets correctly fetches server state during server-side rendering?

function Page({ data }) {
  return <div>{data.message}</div>;
}

// Which data fetching method is correct?
A
export async function getStaticProps() {
  const data = await fetch('https://api.example.com/data').then(res =&amp;gt; res.json());
  return { props: { data } };
}
B
export async function fetchData() {
  const data = await fetch('https://api.example.com/data').then(res =&amp;gt; res.json());
  return data;
}
C
export async function getClientSideProps() {
  const data = await fetch('https://api.example.com/data').then(res =&amp;gt; res.json());
  return { props: { data } };
}
D
export async function getServerSideProps() {
  const res = await fetch('https://api.example.com/data');
  const data = await res.json();
  return { props: { data } };
}
Attempts:
2 left
💡 Hint

Look for the official Next.js data fetching method that runs on the server for every request.

🔧 Debug
advanced
2:00remaining
Debugging Client State Update in Next.js

Given this React component in Next.js, what is the main reason the counter does not update on button click?

import { useState } from 'react';

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

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

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={handleClick}>Increment</button>
    </div>
  );
}
AThe code tries to assign directly to the state variable instead of using the setter function, so React does not re-render.
BThe useState hook is missing an initial value, causing the state to be undefined.
CThe handleClick function is not bound to the button's onClick event properly.
DReact components cannot update state inside event handlers.
Attempts:
2 left
💡 Hint

Remember how React state variables should be updated.

state_output
expert
2:00remaining
Output of Mixed Server and Client State in Next.js

What will be the rendered output of this Next.js page after the user clicks the button once?

import { useState } from 'react';

export async function getServerSideProps() {
  return { props: { initialCount: 5 } };
}

export default function Page({ initialCount }) {
  const [count, setCount] = useState(initialCount);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}
ACount: 5
BCount: 6
CCount: 1
DCount: 0
Attempts:
2 left
💡 Hint

Consider the initial state from server props and what happens after clicking the button.