Next.js mixes server and client code, so managing state needs special care to keep data consistent and fast.
Why state management differs in Next.js
import { useState } from 'react'; // Client component state const [count, setCount] = useState(0); // Server component data fetching const data = await fetchData(); // Using React Server Components and client state together
Next.js uses React Server Components by default, so state hooks like useState only work in client components.
Data fetching can happen on the server, so state management often mixes server data and client state.
// Client component with state 'use client'; import { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); return ( <button onClick={() => setCount(count + 1)}> Count: {count} </button> ); }
// Server component fetching data export default async function Page() { const data = await fetch('https://api.example.com/data').then(res => res.json()); return <div>Data: {data.value}</div>; }
// Mixing server data and client state 'use client'; import { useState } from 'react'; export default function ClientComponent({ initialCount }) { const [count, setCount] = useState(initialCount); return <button onClick={() => setCount(count + 1)}>Count: {count}</button>; }
This is a simple Next.js client component that uses React's useState to manage a counter. It shows how client state works in Next.js.
'use client'; import { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); return ( <main style={{ padding: '1rem', fontFamily: 'Arial, sans-serif' }}> <h1>Simple Counter</h1> <button aria-label="Increase count" onClick={() => setCount(count + 1)} style={{ fontSize: '1.5rem', padding: '0.5rem 1rem', cursor: 'pointer' }} > Count: {count} </button> </main> ); }
Next.js uses React Server Components by default, so you must add 'use client' at the top of files that use client state.
Server components can fetch data before sending HTML to the browser, improving speed.
Client components handle interactive state like clicks or form inputs.
Next.js mixes server and client code, so state management splits between server data and client state.
Use 'use client' directive to enable client state hooks like useState.
Server components fetch data early, client components handle user interaction.
Practice
Solution
Step 1: Understand Next.js architecture
Next.js uses server components to fetch data early and client components for interactivity.Step 2: Identify state management split
State is split because server components cannot hold client state like useState, so client components manage UI state.Final Answer:
Because Next.js uses both server and client components, splitting state handling. -> Option AQuick Check:
Server + Client split = C [OK]
- Thinking Next.js runs only on client like React
- Assuming hooks don't work in Next.js
- Believing class components are required
useState in Next.js components?Solution
Step 1: Recall client directive usage
Next.js requires theuse clientdirective at the top of a file to mark it as a client component.Step 2: Connect directive to hooks
Only client components can use React hooks likeuseStateanduseEffect.Final Answer:
use client-> Option AQuick Check:
Client directive =use client[OK]
- Using 'use server' instead of 'use client'
- Forgetting to add the directive
- Assuming hooks work in server components
"use client";
import { useState } from 'react';
export default function Counter() {
const [count, setCount] = useState(0);
return (
setCount(count + 1)}>
Count: {count}
);
}Solution
Step 1: Confirm client component usage
The"use client";directive enables hooks likeuseStatein this component.Step 2: Understand component behavior
The button shows the current count starting at 0 and increments count on each click.Final Answer:
A button showing 'Count: 0' that increments count on click -> Option DQuick Check:
Client state updates UI = B [OK]
- Thinking useState causes error without 'use client'
- Expecting static output without interaction
- Confusing server and client component roles
export default function Profile() {
const [name, setName] = useState('Guest');
return Hello, {name}
;
}Solution
Step 1: Check for client directive
The component usesuseStatebut lacks the"use client";directive at the top.Step 2: Understand hook usage rules
Hooks likeuseStateonly work in client components marked by"use client";.Final Answer:
Missing 'use client' directive to use useState -> Option BQuick Check:
Hooks need 'use client' directive [OK]
- Assuming useState works without 'use client'
- Thinking initial value type causes error
- Believing function must be async
Solution
Step 1: Separate concerns in Next.js
Server components fetch data early and cannot hold client UI state like input values.Step 2: Use client components for interactivity
Client components withuseStatemanage user input state and handle events.Step 3: Combine server and client components
Use server components for data fetching and client components for interactive UI parts.Final Answer:
Use client components withuseStatefor input, server components for data fetch -> Option CQuick Check:
Split server fetch + client input = D [OK]
- Trying to use useState in server components
- Handling input only on server side
- Avoiding client components for UI state
Ready to prove it?
Pass the test and this pattern moves unknown
