Chapter 9 of 21
Continue learning Next.js step by step.
Client Components
Learn when and how to use Client Components.
Theory
Client Components are used when a component needs browser-side interactivity.
Features such as useState, useEffect and event handlers generally require a Client Component.
A file can be marked as a Client Component by adding "use client" at the top.
Example
"use client";
import { useState } from "react";
export default function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>
);
}Output
Clicking the button increases the displayed counter value.
Practice
Create a counter using useState in a Client Component.
Interview Questions
1
What is a Client Component?
2
When do you use use client?
3
Why are event handlers usually used in Client Components?
Quick Quiz
Which option best describes this chapter?
A.use client
B.client component
C.browser component
D.react client