📖 Scenario: You are building a simple Next.js app that shows a greeting message and a button to toggle the greeting text color. The greeting text color changes only when the user clicks the button.
🎯 Goal: Build a Next.js component that uses a client component to handle user interaction (button click) and state (color toggle) while the rest of the page remains server-rendered.
📋 What You'll Learn
Create a server component that renders a greeting message
Create a client component that toggles the greeting text color on button click
Use React useState hook inside the client component
Use the client component inside the server component
Add a button with an accessible aria-label for toggling color
💡 Why This Matters
🌍 Real World
Many Next.js apps use server components for fast loading and client components for interactive parts like buttons, forms, and toggles.
💼 Career
Understanding when to use client components is essential for building efficient, accessible, and maintainable Next.js applications.
Progress0 / 4 steps
1
Create the Server Component with Greeting Text
Create a server component called GreetingServer that returns a <div> with the text Hello from Server Component! inside a <p> tag.
NextJS
Hint
Server components do not use hooks or client-side state.
2
Create the Client Component with Color Toggle State
Create a client component called ColorToggle that uses useState to toggle a boolean state called isRed starting as false. The component should return a <p> with text Color toggled! styled with red color if isRed is true, otherwise black. Also add a <button> with aria-label="Toggle color" that toggles isRed when clicked.
NextJS
Hint
Client components must start with "use client"; at the top.
3
Import and Use Client Component inside Server Component
Update GreetingServer to render <ColorToggle /> below the greeting text inside the <div>.
NextJS
Hint
Client components can be imported and used inside server components like normal React components.
4
Add Accessibility and Final Touches
Ensure the button in ColorToggle has an aria-label="Toggle color" attribute for accessibility. Also, add a tabIndex="0" to the button to allow keyboard navigation.
NextJS
Hint
Adding tabIndex={0} makes the button reachable by keyboard users.
Practice
(1/5)
1. Which of the following is the main reason to use a client component in Next.js?
easy
A. To make parts of the app interactive and respond to user actions
B. To improve server-side rendering speed
C. To reduce the size of the JavaScript bundle sent to the browser
D. To fetch data from the database on the server
Solution
Step 1: Understand client components purpose
Client components run in the browser and handle interactivity like clicks and animations.
Step 2: Compare options with this purpose
Only To make parts of the app interactive and respond to user actions matches making the app interactive and responding to user actions.
Final Answer:
To make parts of the app interactive and respond to user actions -> Option A
Quick Check:
Client components = interactivity [OK]
Hint: Client components run in browser for interactivity [OK]
Common Mistakes:
Thinking client components improve server rendering
Confusing client components with server data fetching
Believing client components reduce bundle size
2. Which is the correct way to mark a Next.js component as a client component?
easy
A. Wrap the component in <Client></Client> tags
B. Name the file with a .client.js extension
C. Add "use client"; at the top of the file
D. Add client: true in the component props
Solution
Step 1: Recall Next.js client component syntax
Next.js requires "use client"; at the top of the file to mark it as a client component.
Step 2: Check other options
Options A, C, and D are not valid Next.js syntax for client components.
Final Answer:
Add "use client"; at the top of the file -> Option C
Quick Check:
Client component marker = "use client"; [OK]
Hint: Remember: "use client"; at file top marks client component [OK]
Common Mistakes:
Trying to use special tags to mark client components
Using file extensions to indicate client components
Passing props to mark client components
3. What will happen if you try to use useState in a Next.js server component without marking it as a client component?
medium
A. You will get a runtime error because hooks like useState only work in client components
B. The state will be shared across all users
C. The app will render normally without errors
D. The component will automatically become a client component
Solution
Step 1: Understand hook usage rules
Hooks like useState only work in client components because they run in the browser.
Step 2: Check behavior in server components
If you use useState in a server component without "use client" directive, Next.js throws a runtime error.
Final Answer:
You will get a runtime error because hooks like useState only work in client components -> Option A
Quick Check:
useState in server component = runtime error [OK]
Hint: Hooks need client components or error occurs [OK]
Common Mistakes:
Assuming hooks work in server components
Thinking state is shared globally without errors
Believing components auto-switch to client
4. You wrote a Next.js component with "use client"; but forgot to import useState from React. What will happen?
medium
A. The component will render but state updates won't work
B. The component will work fine without errors
C. Next.js will automatically import useState for you
D. You will get a ReferenceError because useState is not defined
Solution
Step 1: Check import requirements for hooks
Hooks like useState must be explicitly imported from React to be used.
Step 2: Analyze missing import effect
Without importing, JavaScript throws a ReferenceError because useState is undefined.
Final Answer:
You will get a ReferenceError because useState is not defined -> Option D
Quick Check:
Missing import useState = ReferenceError [OK]
Hint: Always import hooks before using them [OK]
Common Mistakes:
Assuming Next.js auto-imports React hooks
Ignoring import errors thinking component still works
Confusing runtime errors with silent failures
5. You have a Next.js app with a server component that fetches data and a button that updates UI state on click. How should you organize components?
hard
A. Make the whole component a client component to handle data fetching and button state
B. Keep data fetching in a server component and create a separate client component for the button with state
C. Use only server components and handle button clicks with server actions
D. Fetch data in the client component and handle button state in the server component
Solution
Step 1: Separate concerns for performance and interactivity
Server components are best for data fetching; client components handle interactivity like button clicks.
Step 2: Organize components accordingly
Keep data fetching in server component and create a small client component for the button with state and event handlers.
Final Answer:
Keep data fetching in a server component and create a separate client component for the button with state -> Option B
Quick Check:
Server fetch + client interactivity = separate components [OK]
Hint: Use server for data, client for interactivity [OK]
Common Mistakes:
Making entire component client causing slower loads
Trying to handle UI state in server components
Fetching data in client causing unnecessary delays
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.