DEV Community

Cover image for What is Props Drilling in React.js?
Sudhanshu Gaikwad
Sudhanshu Gaikwad

Posted on • Edited on

What is Props Drilling in React.js?

** What is Props Drilling?**
Props Drilling happens when you pass props from a parent component to deeply nested child components, even if some intermediate components donโ€™t need those props. This makes the code hard to manage and maintain.


Example of Props Drilling
Letโ€™s create a simple React App that demonstrates Props Drilling by passing a message prop from App.js down to a deeply nested component:

1.Create React App

npx create-react-app props-drilling-example
cd props-drilling-example
npm start

Enter fullscreen mode Exit fullscreen mode

2.Update App.js

import React from "react";
import Parent from "./Parent";

function App() {
  return (
    <div>
      <h1>Props Drilling Example ๐Ÿš€</h1>
      <Parent message="Hello from App.js!" />
    </div>
  );
}

export default App;

Enter fullscreen mode Exit fullscreen mode

3.Create Parent.js

import React from "react";
import Child from "./Child";

function Parent({ message }) {
  return <Child message={message} />;
}

export default Parent;

Enter fullscreen mode Exit fullscreen mode

4.Create Child.js

import React from "react";
import GrandChild from "./GrandChild";

function Child({ message }) {
  return <GrandChild message={message} />;
}

export default Child;

Enter fullscreen mode Exit fullscreen mode

5.Create GrandChild.js

import React from "react";

function GrandChild({ message }) {
  return <h2>{message}</h2>;
}

export default GrandChild;

Enter fullscreen mode Exit fullscreen mode

Output:


Flow Diagram Should Look Like This:


Whatโ€™s the Problem?

  • We are passing message prop through multiple components (Parent โ†’ Child โ†’ GrandChild) even though only GrandChildneeds it.
  • This is Props Drilling, and in large apps, it becomes difficult to manage.

How to Fix Props Drilling?
Instead of passing props manually through multiple components, use:

  • React Context API
  • Redux (for global state management)

Props Drilling makes code harder to maintain. Use React Context API or State Management Tools like Redux to avoid unnecessary prop passing.

Top comments (0)