Bird
Raised Fist0
NextJSframework~30 mins

Server state vs client state in NextJS - Hands-On Comparison

Choose your learning style10 modes available
Managing Server State and Client State in Next.js
📖 Scenario: You are building a simple blog page using Next.js. The blog posts come from a server, but you want to let users mark posts as favorites on the client side without sending data back to the server.
🎯 Goal: Build a Next.js component that fetches blog posts as server state and manages favorite posts as client state.
📋 What You'll Learn
Fetch blog posts data from a server using getServerSideProps
Store the fetched posts in a prop called posts
Create a client state variable called favorites to track favorite post IDs
Add a button to toggle favorite status for each post
Render the list of posts with favorite buttons reflecting client state
💡 Why This Matters
🌍 Real World
Many web apps need to show data fetched from servers but also let users interact locally without sending every change back to the server immediately.
💼 Career
Understanding server state vs client state is key for building efficient, user-friendly React and Next.js applications that balance data fetching and user interaction.
Progress0 / 4 steps
1
Setup server state with getServerSideProps
Create an async function called getServerSideProps that returns a props object containing a posts array with these exact objects: { id: 1, title: 'Next.js Basics' } and { id: 2, title: 'React Hooks' }.
NextJS
Hint

Use getServerSideProps to fetch or create data on the server and pass it as props.

2
Add client state for favorites using useState
Inside the default exported component called Blog, create a client state variable called favorites initialized as an empty array using useState.
NextJS
Hint

Use useState to create a client state variable for favorites.

3
Add toggle function to update favorites client state
Inside the Blog component, create a function called toggleFavorite that takes a post id. It should add the id to favorites if not present, or remove it if already present, using setFavorites.
NextJS
Hint

Use includes to check if id is in favorites. Use filter to remove it or spread syntax to add it.

4
Render posts with favorite toggle buttons
Inside the Blog component, return a <main> element containing a list of posts. For each post, render its title and a <button> that calls toggleFavorite(post.id) on click. The button text should be 'Unfavorite' if the post id is in favorites, otherwise 'Favorite'.
NextJS
Hint

Use map to loop over posts and render each with a button that toggles favorite status.

Practice

(1/5)
1. Which of the following best describes server state in Next.js?
easy
A. UI component states like button clicks or form inputs.
B. Temporary data stored only in the browser memory.
C. CSS styles applied to components dynamically.
D. Data fetched from APIs or databases and shared across users.

Solution

  1. Step 1: Understand server state meaning

    Server state is data that comes from the server, such as API responses or database queries, and is shared among users.
  2. Step 2: Compare with client state

    Client state is local to the browser and changes instantly with user actions, unlike server state.
  3. Final Answer:

    Data fetched from APIs or databases and shared across users. -> Option D
  4. Quick Check:

    Server state = shared API data [OK]
Hint: Server state = shared data from backend, not local UI [OK]
Common Mistakes:
  • Confusing server state with client UI state
  • Thinking server state is local to one user
  • Mixing server state with CSS or styling
2. Which syntax correctly uses React's useState hook for client state in Next.js?
easy
A. const [count, setCount] = useState(0)
B. const [data, setData] = useState(fetch('/api/data'))
C. const data = await fetch('/api/data')
D. const count = useState(0)

Solution

  1. Step 1: Identify correct useState syntax

    useState returns an array with current state and a setter function, so destructuring with [state, setState] is correct.
  2. Step 2: Check each option

    const [count, setCount] = useState(0) correctly uses const [count, setCount] = useState(0). const [data, setData] = useState(fetch('/api/data')) wrongly calls fetch inside useState. const data = await fetch('/api/data') is an async fetch, not useState. const count = useState(0) misses destructuring.
  3. Final Answer:

    const [count, setCount] = useState(0) -> Option A
  4. Quick Check:

    useState returns [state, setter] array [OK]
Hint: useState always destructures [value, setter] array [OK]
Common Mistakes:
  • Calling async fetch inside useState
  • Not destructuring useState result
  • Assigning useState directly to a variable
3. Given this Next.js component, what will be displayed initially?
import { useState, useEffect } from 'react';

export default function Example() {
  const [serverData, setServerData] = useState(null);

  useEffect(() => {
    fetch('/api/data')
      .then(res => res.json())
      .then(data => setServerData(data.message));
  }, []);

  return 
{serverData ?? 'Loading...'}
; }
medium
A. undefined
B. Loading...
C. null
D. The message from /api/data immediately

Solution

  1. Step 1: Check initial state value

    The state serverData starts as null, so the component shows 'Loading...' initially because of the nullish coalescing operator.
  2. Step 2: Understand useEffect fetch timing

    The fetch runs after the first render, so the message from the API is not available immediately.
  3. Final Answer:

    Loading... -> Option B
  4. Quick Check:

    Initial state null shows fallback text [OK]
Hint: Initial null state shows fallback before fetch completes [OK]
Common Mistakes:
  • Assuming fetch is synchronous and immediate
  • Expecting undefined or null to render as text
  • Ignoring useEffect timing
4. Identify the bug in this Next.js component that fetches server state and updates client state:
import { useState, useEffect } from 'react';

export default function DataFetcher() {
  const [data, setData] = useState();

  useEffect(async () => {
    const res = await fetch('/api/data');
    const json = await res.json();
    setData(json);
  }, []);

  return <div>{data.message}</div>;
}
medium
A. useEffect callback cannot be async directly
B. Missing initial state value for useState
C. setData should not be called inside useEffect
D. data.message will always be undefined

Solution

  1. Step 1: Check useEffect callback syntax

    React's useEffect callback cannot be async directly; it should call an async function inside.
  2. Step 2: Identify correct fix

    Wrap the async fetch logic inside a separate async function inside useEffect, then call it.
  3. Final Answer:

    useEffect callback cannot be async directly -> Option A
  4. Quick Check:

    Async function inside useEffect, not async callback [OK]
Hint: useEffect callback must be sync; call async function inside [OK]
Common Mistakes:
  • Making useEffect callback async directly
  • Not handling initial undefined state safely
  • Assuming data.message exists before fetch
5. You want to build a Next.js page that shows a list of products fetched from the server and allows users to filter them locally without refetching. Which approach correctly separates server state and client state?
hard
A. Store products in client state only and fetch on every filter change.
B. Fetch products inside useEffect and store in server state for filtering.
C. Fetch products with getServerSideProps and store in client state with useState for filtering.
D. Fetch products with getStaticProps and filter on server on every user input.

Solution

  1. Step 1: Understand server state fetching

    Using getServerSideProps fetches data on the server before rendering, providing server state.
  2. Step 2: Manage client state for filtering

    Storing the fetched products in useState allows local filtering without refetching, which is client state.
  3. Final Answer:

    Fetch products with getServerSideProps and store in client state with useState for filtering. -> Option C
  4. Quick Check:

    Server fetch + client filter state = Fetch products with getServerSideProps and store in client state with useState for filtering. [OK]
Hint: Fetch server data once, filter locally with client state [OK]
Common Mistakes:
  • Fetching data on every filter change
  • Filtering on server for every user input
  • Mixing server and client state responsibilities
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