The Wayback Machine - https://web.archive.org/web/20240914053810/https://www.geeksforgeeks.org/reactjs-state-vs-props/
Open In App

ReactJS State vs Props

Last Updated : 21 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In React props are a way to pass the data or properties from parent component to child components while the state is the real-time data available to use within only that component.

States and props are two of the most important concepts of React and everything in React is based upon them. But before jumping into State and Props we need to know about the relation between components and normal functions.

Relation between Components and normal functions in JavaScript

We know that react components are the building blocks that can be reused again and again in building the UI. Before jumping into the main difference between the state and props, let’s see how a component in react is related to a normal function in javascript.

Example: 

javascript
// simple component 
class FakeComponent extends React.component {
    render() {
        return <div>Hello World!</div>
    }
}
// simple javascript function
const FakeFunction = () => console.log('Hello World!');

In the above code, we declared a simple react component by extending the React.component native method and then we simply render a div that contains ‘Hello World’ inside it as text. After the function we have a simple javascript function inside it which contains a simple console.log that does the same thing inside it, printing ‘Hello World!’. 

So now we know that a React component works similarly to a normal JavaScript function. Let’s take a look at the state 

State in React:

A state is a variable that exists inside a component, that cannot be accessed and modified outside the component, and can only be used inside the component. Works very similarly to a variable that is declared inside a function that cannot be accessed outside the scope of the function in normal javascript. State Can be modified using this.setState. The state can be asynchronous. Whenever this.setState is used to change the state class is rerender itself. Let’s see with the help of an example: 

Props in React:

React allows us to pass information to a Component using something called props (which stands for properties). Props are objects which can be used inside a component. Sometimes we need to change the content inside a component. Props come to play in these cases, as they are passed into the component and the user..

Difference between props and state:

PROPS

STATE

The Data is passed from one component to another.The Data is passed within the component only.
It is Immutable (cannot be modified).It is Mutable ( can be modified).
Props can be used with state and functional components.The state can be used only with the state components/class component (Before 16.0).
Props are read-only.The state is both read and write.

In conclusion, the main significant difference between states and props is that props are used to transfer data from a parent component to a child whereas states are used to manage the data inside a component itself.

Let us see an the example where we can properly understand the difference between state props

Create a React application with the command

npx create-react-app statevsprop 

and write the below code in index.js file of your application

Example: 

JavaScript
// Filename - index.js

import React, { Component } from "react"
import ReactDOM from 'react-dom';
import './index.css';

const Fruit = (props) => {
 
    return (
        <div className="fruit">
            <h1>List of Fruits</h1>
            <p>Name: {props.fruits.name}</p>
            <p>Color: {props.fruits.color}</p>
        </div>
    )
}
 
class Car extends Component {
    constructor() {
        super()
        this.state = {
            car: 'Ferrari'
        }
    }
 
    changeMessage() {
        this.setState({
            car: 'Jaguar'
        })
    }
 
    render() {
        return (
            <div className="App">
                <h1>{this.state.car}</h1>
                <button onClick={() => this.changeMessage()}>
                    Change
                </button>
            </div>
 
        )
    }
}
 

function App() {
 
    const fruits =
    {
        name: "Mango",
        color: "Yellow"
    }
 
    return (
        <div className="App">
            <Fruit fruits={fruits} />
            <hr></hr>
            <Car />
        </div>
    );
}


const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

Output: In the fruit component props are used where data is passed by the parent component(App) to child(Fruit) whereas the car component manages data internally using state

Note: To know about the difference you can check this What are the differences between props and state.



Previous Article
Next Article

Similar Reads

How React handle or restrict Props to certain types, or require certain Props to exist ?
In React, data can be transmitted from a parent component to its children using props. PropTypes, a feature in React, facilitates runtime type checks on these props, issuing warnings in the browser console if the checks fail. This ensures data consistency and helps catch potential issues during development. Syntax: import PropTypes from 'prop-types
2 min read
How Do You Pass Props to `{this.props.children}`?
Passing props is important for sharing data and functionality between parent and child components, facilitating component reusability and maintaining a clear separation of concerns in React applications. In this article, we will explore two different approaches to Pass Props to {this.props.children}. Below are the possible approaches to Props to {t
4 min read
How to send state/props to another component in React with onClick?
The props and state are the main concepts of React. Actually, only changes in props and/ or state trigger React to rerender your components and potentially update the DOM in the browser Props: It allows you to pass data from a parent component to a child component. State: While props allow you to pass data from a parent component to a child compone
2 min read
React.js Blueprint Non-ideal state Component Props
BlueprintJS is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex data-dense for desktop applications. NonIdealState Component provides a way for users to inform the user that some content is unavailable. There are three types of nonideal state : Empty state: To showcase an empt
3 min read
What are the differences between props and state ?
In React JS, the main difference between props and state is that the props are a way to pass the data or properties from one component to other components while the state is the real-time data available to use within only that component. PrerequisitesReact JSReact StatesReact Js PropsLet's understand the differences between props and states using e
4 min read
ReactJS Props - Set 1
Till now we were working with components using static data only. In this article, we will learn about how we can pass information to a Component. What is props?React allows us to pass information to a Component using something called props (which stands for properties). Props are objects which can be used inside a component. Passing and Accessing p
5 min read
ReactJS Props - Set 2
In our previous article ReactJS Props - Set 1 we discussed props, passing and accessing props, passing props from one component to another, etc. In this article, we will continue our discussion on props. So, what if we want to pass some default information using props to our components? React allows us to do so. React provides us with something cal
4 min read
ReactJS Methods as Props
In this article, we will learn about props and passing methods as props. We will also discuss how we can use the child components to pass data to parent components using methods as props. What are props?We know that everything in ReactJS is a component and to pass in data to these components, props are used. Whenever we call child components from p
3 min read
Destructuring of Props in ReactJS
Destructuring is a simple property that is used to make code much clear and readable, mainly when we pass props in React. What is Destructuring? Destructuring is a characteristic of JavaScript, It is used to take out sections of data from an array or objects, We can assign them to new own variables created by the developer.In destructuring, It does
4 min read
ReactJS testInstance.props Property
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In this article, we will know how to use testInstance.props property. The testInstance.props property is used to get a pr
1 min read
How to pass multiple props in a single event handler in ReactJS?
To pass multiple props in a single event handler in ReactJS we can create a function to handle the event that accepts multiple props or calls multiple methods. It is a normal task while creating a react project to perform multiple operations for some events. PrerequisitesReact JSReact JS PropsTo pass/call the multiple props methods in a single even
2 min read
How to pass props to ReactJS component that wrapped in variable ?
To pass props to a React JS component that is wrapped in a variable we have to create some copy of the component that can be modified. As we can't access the props of that component and pass a required argument we can clone it to make the required element and use it in our application. Props: Props means properties. Prop is an argument that we pass
2 min read
How to apply validation on Props in ReactJS ?
Need of Validating Props in React JS Props are used to pass the read-only attributes to React components. For the proper functioning of components and to avoid future bugs and glitches it is necessary that props are passed correctly. Hence, it is required to use props validation to improve the react component's performance. Prerequisites:NodeJS or
3 min read
What is the purpose of using super constructor with props argument in ReactJS ?
React, an open-source JavaScript library for UI design utilizes JSX to render components. Data is passed from parent to child components through props, which can only be updated by the parent. The `super()` function invokes the constructor of the parent class. Prerequisites:NodeJS or NPMReact JSReact PropsSteps to Create the React Application:Step
1 min read
ReactJS Props Reference
In React, components can receive information from a parent component by utilizing props (short for properties). A prop is an object accessible to all React components. It serves as a means to pass data from a parent component to a child component. &lt;Welcome fullName = "Ashish Bhardwaj" /&gt;In the provided code, the prop `fullName` is passed with
2 min read
How to access props.children in a stateless functional component in ReactJS ?
The {props. children} allows you to create a generic template, which can be modified by a parent when invoked. In this way, a parent component can easily pass whatever is necessary to its child, even generated layout features. Anytime you call a component, it automatically displays whatever it has between its opening and closing tags. A component w
5 min read
How to update nested state properties in ReactJS?
There are the following approaches to update nested state properties in ReactJS: Approach 1: We can create a dummy object to perform operations on it (update properties that we want) then replace the component's state with the updated object.Approach 2: We can pass the old nested object using the spread operator and then override the particular pro
2 min read
How to update parent state in ReactJS ?
We can pass a function setting the state from the parent to the child component as a prop. Creating React Application: Step 1: Create a React application using the following command: npx create-react-app foldernameStep 2: After creating your project folder i.e. foldername, move to it using the following command: cd foldernameProject Structure: It w
1 min read
How to set Parent State from Children Component in ReactJS?
We can set Parent State from Children Component in ReactJs using the following approach. Prerequisite: State introduction in ReactJS We will actually set the state of a parent in the parent component itself, but the children will be responsible for setting.We will create a function in parent to set the state with the given input.We will pass that f
3 min read
Lifting State up in ReactJS
Prerequisite: ReactJs, State, Props Lifting up the State: As we know, every component in React has its own state. Because of this sometimes data can be redundant and inconsistent. So, by Lifting up the state we make the state of the parent component as a single source of truth and pass the data of the parent in its children. Time to use Lift up the
2 min read
Unstated Next - Lightweight State Management Library For ReactJS | Part - 2 | Shopping Cart
In the previous published article, we have explained everything you need to know about Unstated Next that recommends you to read that article if you haven't. In this article, we are going to build a simple shopping cart application having the functionality to add, remove, update and display any item in the cart. Prerequisite: Have basic knowledge a
4 min read
Unstated Next - Lightweight State Management Library For ReactJS | Part - 1
You might have used many state management libraries for ReactJs like Redux, Recoil MobX, and even Unstated but have you ever thought you can manage your entire application state just by using 300 bytes of code. It can possible by using the Unstated-next state management library. What is Unstated Next? It is a lightweight state management library bu
4 min read
How to get previous state in ReactJS Functional Component ?
To get the previous state in ReactJS Functional Component we can use the previous value of the state while using the set state method. State can only be directly accessed in the class component so we will be using the react useState hook to accomplish the task. PrerequisitesReact JSReact JS functional componentsReact useState HookSteps to create Re
2 min read
How to save new state to local JSON using ReactJS ?
To save data in local storage is a common task while creating a react application to store the user-specific data. We can save new state to local JSON using react in local storage of the browser and access that information anytime later. Prerequisites:React JSNPM &amp; Node.jsJavaScript localStorageApproach:In React to save a new state to local JSO
2 min read
How to set state with a dynamic key name in ReactJS ?
When working with ReactJS, managing state is a fundamental aspect of building dynamic and interactive user interfaces. In some cases, you may encounter situations where you need to set a state with a dynamic key name, rather than a fixed one. This can be particularly useful when dealing with dynamic data or when you want to update the state based o
2 min read
How to update the State of a component in ReactJS ?
To display the latest or updated information, and content on the UI after any User interaction or data fetch from the API, we have to update the state of the component. This change in the state makes the UI re-render with the updated content. Prerequisites: NPM &amp; Node.jsReact JSReact StateReact JS Class ComponentsReact JS Functional ComponentsA
3 min read
How to lift state two levels up in ReactJS ?
We want to lift the state two levels up such that when an event occurs at the lower level component then it should affect the state two levels up. For example, there are three components X, Y, and Z. X is the parent of Component Y, and Component Y is the parent of Component Z. Prerequisites:NodeJS or NPMReact JSApproach:When some event occurs in co
2 min read
How to update state to re-render the component in ReactJS ?
In React, when the state is updated it initiates to re-render the component and its child components to update the new data on the UI of the application. PrerequisitesReact Functional ComponentsReact Class ComponentsReact HooksUpdate state to re-render the component in React:There are different ways in the functional and class components to update
3 min read
ReactJS State
The State is a way to store and manage the information or data while creating a React Application. The state is a JavaScript object that contains the real-time data or information on the webpage. Table of Content What is React State?Creating State ObjectConventions of Using State in ReactUpdating State in ReactWhat is a React State?The state in Rea
4 min read
How to Use Flux to Manage State in ReactJS?
State management in ReactJS is important for building dynamic and responsive applications. Flux, an architecture for managing state, provides a structured approach to handle data flow and state changes efficiently in React applications. In this article, we will explore the Flux to Manage State in ReactJS with proper examples. Prerequisites:ReactJSJ
5 min read