ReactEasy
What are Client Components?
Client Components are regular interactive React components that are bundled for the browser and hydrated there. In frameworks with Server Components, you mark them by putting "use client" at the top of the file; they are still server-side rendered to HTML for the first load, then can use state, effects, event handlers, context and browser APIs.
When you need one
useState,useReducer,useEffect,useReffor DOM work.- Event handlers (
onClick,onChange). - Context consumers and providers.
- Browser APIs:
localStorage,window, geolocation,IntersectionObserver. - Client hooks:
useActionState,useOptimistic,useFormStatus, router hooks. - Libraries built on hooks (charts, animation, form libraries).
The boundary
"use client"marks a module boundary: that file and everything it imports become client code.- Props from Server Components must be serializable (plain data, JSX, promises, Server Functions), not regular functions or class instances.
- Client Components cannot import Server Components, but can render them when passed as
childrenor other props.
Best practices
- Keep Client Components small and at the leaves (a button, a form, a chart), so most of the page stays server-rendered.
- Pass server data as props instead of fetching again on the client.
- Do not add
"use client"to layouts or pages by habit.
Outside frameworks
In a Vite SPA every component is effectively a Client Component, and the directive is not needed.
Code example
tsx
1// app/components/search-filter.tsx2"use client"; // client boundary: state + events + browser APIs34import { useState } from "react";56type Item = { id: string; name: string };78export function SearchFilter({ items, children }: { items: Item[]; children?: React.ReactNode }) {9 const [query, setQuery] = useState(() => sessionStorage.getItem("q") ?? "");10 const visible = items.filter((i) => i.name.toLowerCase().includes(query.toLowerCase()));1112 return (13 <section>14 <input15 value={query}16 onChange={(e) => {17 setQuery(e.target.value);18 sessionStorage.setItem("q", e.target.value);19 }}20 placeholder="Filter"21 />22 <ul>{visible.map((i) => <li key={i.id}>{i.name}</li>)}</ul>23 {children} {/* can be server-rendered content passed from a Server Component */}24 </section>25 );26}2728// Server Component usage:29// const items = await db.item.findMany();30// <SearchFilter items={items.map(({ id, name }) => ({ id, name }))}><ServerFooter /></SearchFilter>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.