ReactEasy

What is prop drilling?

Prop drilling is passing props through several layers of components that do not use them, only so a deeply nested component can receive them. It makes code verbose, couples intermediate components to data they do not care about, and makes refactoring harder.

Why it happens

State is lifted to a high-level component (often the app root), but the consumer is many levels down: App > Layout > Sidebar > Menu > UserBadge.

Is it always bad?

No. Passing props two or three levels is explicit and easy to follow. It becomes a problem when many intermediate components forward the same props.

Solutions

  1. Composition: pass components as children or slot props so the parent renders the consumer directly. Often the simplest fix.
  2. Context: createContext plus useContext (or React 19's use(Context)) for values many components need: theme, current user, locale. Render <UserContext value={user}> directly in React 19.
  3. State libraries: Zustand, Jotai or Redux Toolkit for frequently changing global state with selective subscriptions.
  4. Server state libraries: TanStack Query or SWR let any component read cached server data by key.
  5. Frameworks: Server Components can fetch data right where it is used.

Context caveat

Every consumer re-renders when the context value changes. Split contexts or use a store when values change often.

Code example

tsx
1import { createContext, use, useState } from "react";
2
3type User = { name: string };
4
5// Drilling: Layout and Sidebar only forward `user`
6// <Layout user={user}><Sidebar user={user}><UserBadge user={user} /></Sidebar></Layout>
7
8// Fix with context (React 19 syntax)
9const UserContext = createContext<User | null>(null);
10
11function UserBadge() {
12 const user = use(UserContext); // read directly, no drilling
13 return <span>{user?.name ?? "Guest"}</span>;
14}
15
16function Sidebar() {
17 return <aside><UserBadge /></aside>; // no user prop needed
18}
19
20export function App() {
21 const [user] = useState<User>({ name: "Ada" });
22 return (
23 <UserContext value={user}>
24 <Sidebar />
25 </UserContext>
26 );
27}

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.

Open in study app