When to Use Client Components in Next.js
📖 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 componentUse 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