Loading...
Loading...
Prop Drilling is a situation when data (props) is passed through multiple levels of nested components, even if intermediate components don't use them directly.
Example:
function App() {
const user = { name: "John" };
return <Parent user={user} />;
}
function Parent({ user }) {
return <Child user={user} />;
}
function Child({ user }) {
return <p>Hello, {user.name}!</p>;
}
Here Child uses user, but it's passed through Parent which doesn't use it itself — this is Prop Drilling.
Context allows passing data to any nesting level, bypassing intermediate components.
const UserContext = createContext(null);
function App() {
const user = { name: "John" };
return (
<UserContext.Provider value={user}>
<Parent />
</UserContext.Provider>
);
}
function Parent() {
return <Child />;
}
function Child() {
const user = useContext(UserContext);
return <p>Hello, {user.name}!</p>;
}
Suitable for more complex applications with lots of data needed in different parts.
// Zustand example
const useUserStore = create((set) => ({
user: { name: "John" },
}));
function Child() {
const user = useUserStore((state) => state.user);
return <p>Hello, {user.name}!</p>;
}
Sometimes props can be passed not directly but using functions or custom hooks that encapsulate logic.
When Prop Drilling is acceptable:
If passing 1-2 levels deep — it's normal. Use context or global state only if really needed.