📖 Scenario: You are building a simple interactive button in a Next.js app. This button will count how many times it has been clicked. You want to understand why this button must be a client component to handle the clicks and update the count.
🎯 Goal: Create a Next.js client component that shows a button and a count. Each click on the button increases the count by one. This will demonstrate why client components handle interactivity.
📋 What You'll Learn
Create a React functional component named ClickCounter.
Use the useState hook to store the count.
Add a button that increments the count when clicked.
Mark the component as a client component using 'use client' directive.
💡 Why This Matters
🌍 Real World
Interactive buttons and UI elements are common in web apps for user input and feedback.
💼 Career
Understanding client components and interactivity is essential for building modern React and Next.js applications.
Progress0 / 4 steps
1
Create the basic component structure
Create a React functional component named ClickCounter that returns a div with the text Clicks: 0 inside.
NextJS
Hint
Start by writing a function named ClickCounter that returns a div with the text Clicks: 0.
2
Add state to track clicks
Inside the ClickCounter component, import and use the useState hook to create a state variable called count initialized to 0. Update the returned div to display the current count.
NextJS
Hint
Use useState(0) to create count and setCount. Show {count} inside the div.
3
Add button to increment count
Add a button element inside the returned JSX of ClickCounter. Set its onClick handler to a function that calls setCount(count + 1). The button text should be Click me.
NextJS
Hint
Add a button with an onClick that increases count by 1.
4
Mark the component as a client component
Add the directive 'use client' as the first line in the file before any imports or code to mark ClickCounter as a client component.
NextJS
Hint
Place 'use client'; at the very top of the file to enable client-side interactivity.
Practice
(1/5)
1. Why do client components in Next.js handle interactivity like clicks and typing?
easy
A. Because they only render static content
B. Because they run on the server to improve performance
C. Because they do not use React hooks
D. Because they run in the browser where user actions happen
Solution
Step 1: Understand where interactivity happens
User actions like clicks and typing happen in the browser, not on the server.
Step 2: Identify where client components run
Client components run in the browser, so they can respond to user actions directly.
Final Answer:
Because they run in the browser where user actions happen -> Option D
Quick Check:
Interactivity needs browser execution = A [OK]
Hint: Interactivity needs browser code, so client components handle it [OK]
Common Mistakes:
Thinking server components handle clicks
Believing client components run on the server
Confusing static rendering with interactivity
2. Which syntax correctly marks a Next.js component as a client component to enable interactivity?
easy
A. Use export default clientComponent() instead of export default
B. Wrap the component in <Client></Client> tags
C. Add 'use client' at the top of the component file
D. Add interactive=true inside the component function
Solution
Step 1: Recall how to enable client components
Next.js requires the special directive 'use client' at the top of the file to mark it as a client component.
Step 2: Check the options for correct syntax
Only Add 'use client' at the top of the component file uses the exact correct syntax: 'use client' at the top.
Final Answer:
Add 'use client' at the top of the component file -> Option C
Quick Check:
Directive 'use client' = A [OK]
Hint: Look for 'use client' directive at file top to enable client component [OK]
Common Mistakes:
Trying to wrap component in special tags
Using incorrect export syntax
Adding props instead of directive
3. What will be the output when clicking the button in this client component?
A. The onClick handler calls setOn immediately instead of on click
B. Missing 'use client' directive at the top
C. useState is not imported correctly
D. Button text should be uppercase only
Solution
Step 1: Analyze the onClick handler syntax
The onClick is set to setOn(!on), which calls the function immediately during render instead of on click.
Step 2: Correct usage of onClick
It should be a function: onClick={() => setOn(!on)} to delay execution until click.
Final Answer:
The onClick handler calls setOn immediately instead of on click -> Option A
Quick Check:
onClick needs a function, not direct call = B [OK]
Hint: Wrap setState calls in arrow function for onClick [OK]
Common Mistakes:
Calling setState directly in onClick
Forgetting 'use client' directive
Ignoring function syntax for event handlers
5. You want to create a client component that toggles a dark mode theme on button click. Which approach correctly combines client component rules and React hooks?