In Next.js, which of the following best describes server state compared to client state?
Think about where the data lives and when it is fetched or updated.
Server state is data fetched on the server (e.g., via getServerSideProps) and sent as HTML to the client. Client state is data managed inside the browser, often with React hooks like useState, and can change without a full page reload.
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?
Think about how initial data is loaded and how user interactions update the UI.
Next.js fetches the product list on the server and sends it as props. The client manages filtering locally using React state, so no extra server fetch is needed for filtering.
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?Look for the official Next.js data fetching method that runs on the server for every request.
getServerSideProps runs on the server on every request and fetches data to pass as props. getStaticProps runs at build time, and getClientSideProps does not exist.
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>
);
}Remember how React state variables should be updated.
React state variables are read-only. To update state, you must call the setter function returned by useState. Direct assignment to the state variable does not trigger re-render.
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>
);
}Consider the initial state from server props and what happens after clicking the button.
The initial count is 5 from server props. Clicking the button calls setCount(count + 1), so the count updates to 6 and re-renders.