Open In App

ReactJS State vs Props

Last Updated : 11 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In React, State allows components to manage and update internal data dynamically, while Props enables data to be passed from a parent component to a child component. Understanding their differences and use cases is essential for developing efficient React applications.

State in React

State is a built-in object in React components that holds data or information about the component. It is mutable, which means it can be updated within the component using the setState method in class components or the useState hook in functional components.

  • State is local to the component and cannot be accessed by child components unless passed down as props.
  • It is mutable, meaning it can change over time based on user interactions or API responses.
  • When state updates, the component re-renders to reflect the changes.
  • Managed using useState in functional components or this.setState in class components.
JavaScript
import React, { useState } from 'react';

function Counter() {
    const [count, setCount] = useState(0);

    return (
        <div>
            <p>Count: {count}</p>
            <button onClick={() => setCount(count + 1)}>Increment</button>
        </div>
    );
}

export default Counter;

Output

count

State in React

In this example

  • useState(0) initializes the state variable count with the value 0.
  • The setCount function is used to update the state whenever the button is clicked. This triggers a re-render, updating the displayed count.

Props in React

Props (short for Properties) are used to pass data from a parent component to a child component. Unlike state, props are immutable, meaning they cannot be modified within the receiving component.

  • Props allow components to be reusable and dynamic.
  • Props are read-only and cannot be changed by the child component.
  • They help in data communication between components.
  • Passed as attributes in JSX elements.
JavaScript
import React from 'react';

function Greeting({ name }) {
    return <h1>Hello, {name}!</h1>;
}

function App() {
    return <Greeting name="Jiya" />;
}

export default App;

Output

Props-in-React

Props in React

Here, the Greeting component receives a prop named name and displays a personalized message. The App component passes the value “Jiya” as a prop.

Difference between State and Props in React

Aspect

State

Props

Modification

Can be changed by the component itself

Cannot be changed by the receiving component

Communication

Facilitates communication within a component

Facilitates communication between components

Re-rendering

Changes in state trigger a re-render of the component where the state is modified.

Changes in props cause a re-render of the component, but the parent component is responsible for managing props.

Component Type

Props can be used in both class and functional components.

State is primarily used in class components (via this.state and this.setState) or functional components with hooks (useState).

Effect on Parent

Changing props doesn’t affect the parent component.

Changing state only affects the component where the state is defined.

Responsibility

Managed by the component

Managed by the parent component

Examples

<Button color=”blue” onClick={handleClick} />

const [count, setCount] = useState(0);

When to Use State and Props?

  • Use State when you need to manage data that can change over time within a component (e.g., form inputs, counters, UI toggles).
  • Use Props when you need to pass data from a parent component to a child component to make components reusable.

Conclusion

In ReactJS, both State and Props play important roles in managing data and rendering UI dynamically. Props are used to pass data from a parent component to a child component, ensuring a unidirectional flow of information. They are immutable, means a child component cannot modify the props it receives. On the other hand, State is used to store and manage data within a component that can change over time.

  • Unlike props, state is mutable and allows components to re-render when updated, making it essential for handling dynamic content and user interactions.
  • While props help in passing external data and making components reusable, state is crucial for managing internal component behavior.
  • Understanding the difference between State and Props ensures better maintainability, reusability, and efficiency in React applications.


Next Article

Similar Reads

three90RightbarBannerImg
  翻译: