📖 Scenario: You are building a simple Next.js app that shows a counter. You want to understand how state works differently in Next.js compared to regular React apps.
🎯 Goal: Create a Next.js component that uses state to count button clicks. Learn how to set up state with useState and see how server and client parts behave differently.
📋 What You'll Learn
Create a functional component called Counter
Use useState to create a state variable count starting at 0
Add a button that increases count by 1 when clicked
Understand that state updates happen only on the client side in Next.js
Add a note explaining that server components do not hold state
💡 Why This Matters
🌍 Real World
Next.js apps often mix server and client code. Understanding where state lives helps build fast and interactive web apps.
💼 Career
Knowing state management in Next.js is key for frontend developers working with modern React frameworks and building scalable web applications.
Progress0 / 4 steps
1
Create the Counter component with initial state
Create a functional component called Counter in Next.js. Inside it, use useState to create a state variable named count with initial value 0.
NextJS
Hint
Remember to import useState from 'react' and call it inside your component.
2
Add a button to increase the count
Inside the Counter component, add a <button> element. When clicked, it should call setCount(count + 1) to increase the count state by 1. Also display the current count inside the button.
NextJS
Hint
Use the onClick event on the button to update the state.
3
Explain client-side state updates in Next.js
Add a paragraph below the button inside the Counter component that says: "State updates happen only on the client side in Next.js."
NextJS
Hint
Use a paragraph tag to add the note below the button.
4
Add a note about server components and state
Add another paragraph below the first one inside the Counter component that says: "Server components in Next.js do not hold state."
NextJS
Hint
Add a second paragraph below the first to explain server components.
Practice
(1/5)
1. Why does state management in Next.js differ from traditional React apps?
easy
A. Because Next.js uses both server and client components, splitting state handling.
B. Because Next.js only runs on the client side like React.
C. Because Next.js does not support hooks like useState.
D. Because Next.js requires class components for state.
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 A
Quick Check:
Server + Client split = C [OK]
Hint: Remember: Next.js mixes server and client code [OK]
Common Mistakes:
Thinking Next.js runs only on client like React
Assuming hooks don't work in Next.js
Believing class components are required
2. Which directive enables client-side state hooks like useState in Next.js components?
easy
A. use client
B. use server
C. client state
D. enable hooks
Solution
Step 1: Recall client directive usage
Next.js requires the use client directive 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 like useState and useEffect.
Final Answer:
use client -> Option A
Quick Check:
Client directive = use client [OK]
Hint: Look for 'use client' at file top for hooks [OK]
Common Mistakes:
Using 'use server' instead of 'use client'
Forgetting to add the directive
Assuming hooks work in server components
3. Given this Next.js component code, what will be the rendered output?