What Are Props In React.js (Next.js). Should I Follow Or Avoid Props Drilling.

  • Posted on October 4, 2023
  • Technology
  • By MmantraTech
  • 213 Views

In React.js, "props" is short for properties and is a mechanism for passing data from one component to another. Props are read-only, which means that the data coming from props cannot be modified by the child components that receive them. This helps in maintaining the unidirectional data flow, which is a core concept in React.

Thumnail-Python-If-Else (22)-w9JjUl4vlN.png

Understand Props Funda in one umbrella by below diagram.

 

import React from 'react';

// Parent Component
const Calculator = () => {
  
  return (<>
    
    <Multiply a="10" b="34" />;
    <Sum a="10" b="34" />;

    </>)
  
};

// Child Component
const Multiply = (props) => {
  return <div>{props.a * props.b}</div>;
};
const Sum = (props) => {
  return <div>{props.a + props.b}</div>;
};

export default Calculator;

 

Explanation:

  1. Calculator is the parent component that defines a variable x and y variable with the value "10" and "34".
  2. Calculator  renders ChildComponent and passes the x and y  variable as a prop named.
  3. ChildComponent receives the variables  as a  prop and calculate needed operations and displays it inside a <div> element.

This way, data is passed from the parent component to the child component using props, enabling the reusability of components and maintaining a clear data flow.

------ Props drilling -------

Props drilling, also known as prop threading or prop chaining, is a concept in React where data is passed through multiple layers of components within an application. This scenario arises when you have a deeply nested component structure, and data from a top-level component needs to be accessed by a component deeply nested in the hierarchy.

// Grandparent Component
const Grandparent = () => {
  const data = "Hello from Grandparent!";
  return <Parent data={data} />;
};

// Parent Component
const Parent = (props) => {
  return <Child data={props.data} />;
};

// Child Component
const Child = (props) => {
  return <Grandchild data={props.data} />;
};

// Grandchild Component
const Grandchild = (props) => {
  return <div>{props.data}</div>;
};

 

In this example, the data prop originates in the Grandparent component and is passed down through the Parent component, then to the Child component, and finally to the Grandchild component, where it is displayed.

However, it's important to note that while props drilling works, it can lead to complexities in large applications. Managing data flow becomes challenging, making it harder to maintain and debug the code. Moreover, it might cause performance issues due to unnecessary re-renders triggered by changes in props that don't affect the current component.

To tackle these challenges and improve code maintainability, React developers often opt for state management solutions like the React Context API or external state management libraries such as Redux. These tools allow for centralized state management, eliminating the need for props drilling. By using context or state management libraries, developers can simplify the component hierarchy and enhance the overall efficiency of their applications.

1
Author
No Image
Admin
MmantraTech

Mmantra Tech is a online platform that provides knowledge (in the form of blog and articles) into a wide range of subjects .

You May Also Like

Write a Response