Difference between Props and State in ReactJs

Table of contents

No heading

No headings in the article.

In React JS, both props and state are used to manage the dynamic behaviour of components, but they have different characteristics and purposes:

  1. Props (short for "properties") are passed down to a component from its parent component. They are read-only and cannot be modified by the component itself. Props are used to pass data and functions between components.

For example, consider a parent component that renders a child component and passes a prop to it:

import React from 'react'

function Child(props) {
  return (
  <h1>Hello {props.name}</h1>
  // props.name = "Sid"   
  //throw an error,you can't reassign values to a variable in React
  )
}

export default Child
import './App.css';
import Child from './components/Child';

function App() {
 return (
   <>
     <Child name={Disha}/>
     <Child name={Mahi}/>
   </>
 );
}
//Output-
Hello Disha
Hello Mahi

In this example, the Parent component passes a prop called name with the value of "Disha" and "Mahi" to the Child component. The Child component receives the prop as a parameter and uses it to render a personalized greeting.

State, on the other hand, is managed within a component and can be modified by the component itself. State represents the current condition of a component and is used to implement dynamic behavior.

  1.   import React, { useState } from "react";
    
      function Counter() {
        // Declare a state variable called "count" and initialize it to 0
        const [count, setCount] = useState(0);
    
        // Define a function to handle the "increment" button click
        const handleIncrement = () => {
          // Update the "count" state by adding 1 to the previous value
          setCount(count + 1);
        };
    
        // Define a function to handle the "decrement" button click
        const handleDecrement = () => {
          // Update the "count" state by subtracting 1 from the previous value
          setCount(count - 1);
        };
    
        return (
          <div>
            <h2>Count: {count}</h2>
            <button onClick={handleIncrement}>Increment</button>
            <button onClick={handleDecrement}>Decrement</button>
          </div>
        );
      }
    
      export default Counter;
    

    In this example, the useState hook is used to declare a state variable called "count" and initialize it to 0. The hook returns an array with two elements: the current state value (in this case, "count") and a function to update the state value (in this case, "setCount").

    Two additional functions are defined to handle the "increment" and "decrement" button clicks, respectively. These functions update the "count" state by calling the "setCount" function with the new value (i.e., the previous value plus or minus )

    Finally, the component returns a simple UI with the current count value and two buttons to increment or decrement the count. Whenever the count state is updated, the component will re-render with the new value.

--> In summary, props are used to pass data and functions between components, while state is used to manage the dynamic behaviour of a single component.

Understanding the differences between props and state is essential in building scalable and maintainable React applications. By using props and state effectively, developers can create modular and reusable components that can be easily composed to build complex user interfaces.

Here are some differences between props and state in React JS:

  1. Source of Data: Props are passed down from parent components to child components, whereas state is defined within a component itself.

  2. Mutability: Props are read-only and cannot be modified by the component that receives them. State, on the other hand, can be modified using the setState() method.

  3. Initialization: Props are initialized by the parent component and are passed down to the child component as an argument. State is initialized within the component's constructor or by using the useState hook.

  4. Scope: Props are accessible by the component that receives them as well as any child components. State is only accessible within the component that defines it.

  5. Type: Props can be of any data type, including objects, arrays, functions, and primitives, while state is typically an object or a set of variables.

  6. Role: Props are used for communication between components, whereas state is used to manage the internal state of a component.

  7. Debugging: Debugging props is typically easier than debugging state, as the flow of data is more straightforward, and the source of changes is typically more predictable. Debugging state can be more challenging as state changes can be triggered by a wide range of user interactions and lifecycle methods.