Skip to content
Related Articles
Open in App
Not now

Related Articles

What is prop drilling and how to avoid it ?

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 24 Jun, 2021
Improve Article
Save Article

What are props?

Components in React can be passed some parameters. These parameters are generally named props. There is no hard and fast rule that they should be mentioned as props, but it is convenient to use the same convention.

What is Prop Drilling?

Anyone who has worked in React would have faced this and if not then will face it definitely. Prop drilling is basically a situation when the same data is being sent at almost every level due to requirements in the final level. Here is a diagram to demonstrate it better. Data needed to be sent from Parent to ChildC. In this article different ways to do that are discussed.

Creating React Application:

  • Step 1: Create a React application using the following command:

    npx create-react-app useContextReact 
  • Step 2: After creating your project folder i.e. useContextReact, move to it using the following command:

    cd useContextReact

Project Structure: It will look like the following.

Example 1: With using Prop Drilling

without_useContext.js




import React, { useState } from "react";
  
function Parent() {
  const [fName, setfName] = useState("firstName");
  const [lName, setlName] = useState("LastName");
  return (
    <>
      <div>This is a Parent component</div>
      <br />
      <ChildA fName={fName} lName={lName} />
    </>
  );
}
  
function ChildA({ fName, lName }) {
  return (
    <>
      This is ChildA Component.
      <br />
      <ChildB fName={fName} lName={lName} />
    </>
  );
}
  
function ChildB({ fName, lName }) {
  return (
    <>
      This is ChildB Component.
      <br />
      <ChildC fName={fName} lName={lName} />
    </>
  );
}
  
function ChildC({ fName, lName }) {
  return (
    <>
      This is ChildC component.
      <br />
      <h3> Data from Parent component is as follows:</h3>
      <h4>{fName}</h4>
      <h4>{lName}</h4>
    </>
  );
}
  
export default Parent;


 

App.js




import "./styles.css";
import Parent from "./without_useContext";
  
export default function App() {
  return (
    <div className="App">
      <Parent />
    </div>
  );
}


Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Demonstrating the Data initialized in Parent, Needed in last component(Child C) have to passed down each level known as Prop Drilling.

Example 2: Without Prop Drilling

The problem with Prop Drilling is that whenever data from the Parent component will be needed, it would have to come from each level, Regardless of the fact that it is not needed there and simply needed in last.

A better alternative to this is using useContext hook. The useContext hook is based on Context API and works on the mechanism of Provider and Consumer. Provider needs to wrap components inside Provider Components in which data have to be consumed. Then in those components, using the useContext hook that data needs to be consumed.

with_useContext.js




import React, { useState, useContext } from "react";
  
let context = React.createContext(null);
function Parent() {
  const [fName, setfName] = useState("firstName");
  const [lName, setlName] = useState("LastName");
  return (
    <context.Provider value={{ fName, lName }}>
      <div>This is a Parent component</div>
      <br />
      <ChildA />
    </context.Provider>
  );
}
  
function ChildA() {
  return (
    <>
      This is ChildA Component.
      <br />
      <ChildB />
    </>
  );
}
  
function ChildB() {
  return (
    <>
      This is ChildB Component.
      <br />
      <ChildC />
    </>
  );
}
  
function ChildC() {
  const { fName, lName } = useContext(context);
  return (
    <>
      This is ChildC component.
      <br />
      <h3> Data from Parent component is as follows:</h3>
      <h4>{fName}</h4>
      <h4>{lName}</h4>
    </>
  );
}
  
export default Parent;


App.js




import "./styles.css";
import Parent from "./with_useContext";
  
export default function App() {
  return (
    <div className="App">
      <Parent />
    </div>
  );
}


Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
 

Same output but this time instead of passing data through each level, It is directly consumed in the component required using useContext Hook.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!