📖 Scenario: You are building a simple interactive counter on a webpage using Next.js. The counter will let users increase the number by clicking a button.
🎯 Goal: Create a client component in Next.js that uses state and hooks to display a number and a button. When the button is clicked, the number increases by one.
📋 What You'll Learn
Use a client component with the "use client" directive
Create a state variable called count initialized to 0
Add a button that increments count by 1 when clicked
Display the current count value in a <p> tag
💡 Why This Matters
🌍 Real World
Interactive counters are common in web apps for likes, votes, or quantity selectors in shopping carts.
💼 Career
Understanding state and hooks in client components is essential for building dynamic user interfaces in Next.js and React.
Progress0 / 4 steps
1
Create a client component with initial count state
Create a new React functional component called Counter with the "use client" directive at the top. Inside it, use the useState hook to create a state variable called count initialized to 0.
NextJS
Hint
Remember to add "use client" at the top to make this a client component. Use useState(0) to create the count state variable.
2
Add a button to increment the count
Inside the Counter component, add a <button> element. Set its onClick handler to a function that calls setCount(count + 1) to increase the count by one.
NextJS
Hint
Use an arrow function inside onClick to call setCount(count + 1).
3
Display the current count value
Modify the Counter component to display the current count value inside a <p> tag above the button.
NextJS
Hint
Use curly braces inside the <p> tag to show the count value.
4
Export the Counter component as default
Add a default export statement for the Counter component at the end of the file.
NextJS
Hint
Use export default Counter to make this component usable in other files.
Practice
(1/5)
1. What is the main purpose of using useState in a Next.js client component?
easy
A. To fetch data from the server before rendering
B. To store and update data that changes over time within the component
C. To style the component dynamically
D. To navigate between pages automatically
Solution
Step 1: Understand what useState does
useState is a React hook that allows components to keep track of data that can change, like user input or toggles.
Step 2: Identify the purpose in Next.js client components
In Next.js client components, useState helps remember and update values without reloading the page.
Final Answer:
To store and update data that changes over time within the component -> Option B
Quick Check:
State = Remember and update data [OK]
Hint: Remember: useState stores changing data inside components [OK]
Common Mistakes:
Thinking useState fetches server data
Confusing useState with styling or navigation
Forgetting useState only works in client components
2. Which of the following is the correct way to declare a state variable called count with initial value 0 in a Next.js client component?
Hooks like useState require the component to be a client component, which needs 'use client'; at the top.
Step 2: Verify code for missing directive
The code lacks 'use client'; at the top, so it will cause an error in Next.js.
Final Answer:
Missing 'use client'; directive at the top -> Option D
Quick Check:
Client components need 'use client' [OK]
Hint: Always add 'use client'; on top when using hooks [OK]
Common Mistakes:
Forgetting 'use client' directive
Thinking useState syntax is wrong
Assuming onClick handler is invalid
5. You want to create a Next.js client component that toggles between showing and hiding a message when a button is clicked. Which code snippet correctly implements this behavior?