Understanding React State and Data Flow
Last Updated :
24 Apr, 2025
React, a very popular JavaScript library used for building user interfaces has the concept of components and they can manage the states efficiently. ReactJs is based on the concept of uni-directional data transfer. In this article, we will understand React State and Data Flow across the application.
What is React State?
A state in React is a special object that represents the current state of a component. Unlike props, which are passed from parent to child components, state is internal and can be changed by the component itself. State is used to manage dynamic data and to trigger re-rendering of components when the state changes. Each component can have its own state, making it a self-contained unit with its own data.
Setting State:
State can be initialised using the useState hook in functional components or via the this.state and this.setState methods in class components.
Example: Creating a counter button to count the number of times the button is clicked.
JavaScript
// Functional component using with useState hook
import React, { useState } from "react";
function Counter() {
// useState hook
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
// incrementing the value of the count by 1
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
export default Counter;
Output:
OutputState in Class Components: In class components, state is initialized in the constructor, and setState is used to update it:
Example: Creating a counter button to count the number of times the button is clicked.
JavaScript
// Class component with state
import React, { Component } from 'react';
class Counter extends Component {
constructor(props) {
super(props);
this.state = {
count: 0,
};
}
render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Increment
</button>
</div>
);
}
}
export default Counter;
Output:
OutputData Flow in React JS:
React follows a unidirectional data flow, meaning that data flows in a single direction through the components. This helps in maintaining a predictable state and making it easier to understand how changes affect the application. In React we can pass props from Parent component to child and with the help of callback can also pass props from child to parent.
1. Parent to Child:
Data is passed from parent components to child components through props. The parent component owns the state, and it passes down relevant data to its child components as props.
Props (short for "properties") in React are a mechanism for passing data from a parent component to its child component as a set of read-only attributes.
For example: Sending the data from parent to child as "Hello, this is data from the parent to child".
JavaScript
// Parent component
import React, { useState } from 'react';
import ChildComponent from './ChildComponent';
function ParentComponent() {
const [data, setData] = useState('"Hello, this is the data from the parent to child".!');
// passing the data to child as props
return <ChildComponent data={data} />;
}
export default ParentComponent;
JavaScript
// Child component
import React from 'react';
// the child is receiving the data as props
function ChildComponent(props) {
return <p>{props.data}</p>;
}
export default ChildComponent;
Output:

2. Child to Parent:
To communicate from child to parent, the parent can pass down a callback function as a prop to the child. The child can then invoke this function to send data back to the parent.
Example: Sending the data from child to parent as "Hello, this is data from the child to parent".
JavaScript
// Parent component
import React, { useState } from "react";
import ChildComponent from "./ChildComponent";
function ParentComponent() {
const [dataFromChild, setDataFromChild] = useState("");
const handleDataFromChild = (data) => {
setDataFromChild(data);
};
return (
<div>
<p>Data from child: {dataFromChild}</p>
<ChildComponent sendDataToParent={handleDataFromChild} />
</div>
);
}
export default ParentComponent;
JavaScript
// Child component
import React from "react";
function ChildComponent(props) {
const sendData = () => {
props.sendDataToParent("Hello from child!");
};
return <button onClick={sendData}>Send Data to Parent</button>;
}
export default ChildComponent;
Output:
Output
Similar Reads
ReactJS Unidirectional Data Flow
In ReactJS, unidirectional data flow means that data moves in a single directionâfrom the parent component to child componentsâvia props. Changes to the state are always initiated in the parent and propagated downward. Any feedback or data from the child component to the parent is achieved using cal
4 min read
Understanding Node.js Async Flows
Node.js is a powerful runtime environment for building server-side applications. One of the key features of Node.js is its ability to handle asynchronous flows, which allows it to process multiple requests concurrently and improve the performance of web applications. Async flows in Node.js are achie
5 min read
State Transition Matrix and Diagram
State Transition Matrices and Diagrams are powerful tools used in the various disciplines including the computer science, engineering and control systems to model the behavior of the systems that transition between the different states. Understanding these concepts is crucial for the students and pr
9 min read
State Reduction and State Assignment
To illustrate the process of state reduction and state assignment first we have to know the concepts of the state diagram, state table, and state equation. In this article, we are going to learn all the topics related to state reduction and assignment.What is State Diagram?The state graph or state d
7 min read
3 Important Things to Know About State in React
React... We all know the popularity of this framework among developers or programmers. React sit on the top of all the popular JavaScript library. It is really important to understand the concept of React and its different segments. State which is the heart of React is the most important concept to
6 min read
Important Properties of State Transition Matrix
A state transition matrix is ââa fundamental concept used to describe the Fundamental evolution of a linear time-invariant system in a state space representation. The state transition matrix is often represented by Ф(t). In this article, we will Go Through What is State Transition Matrix, What is Li
11 min read
Lifting State Up in ReactJS
In ReactJS, "lifting state up" is a fundamental concept that plays an important role in managing the state efficiently across different components. When building React applications, it is common for multiple components to need access to the same piece of data or state. In such cases, instead of dupl
4 min read
Winning and Losing States for Competitive Programming
In Competitive Programming, we often encounter problems where two players are playing a game optimally and we need to find the winner of the game. In order to solve such problems, we should have a clear understanding of Winning and Losing states of the game. In this article, we will go through the b
4 min read
What's the typical flow of data like in a React with Redux app ?
Redux is an open-source state management JavaScript library for managing the application state. It is popularly used in ReactJS, but is not limited to it, and can also be used with other JavaScript libraries such as Angular. In a conventional React-Redux application, there is a single store along wi
5 min read
State management using Redux in Flutter
State management is a crucial aspect of building dynamic and responsive applications. While building applications, we sometimes need to maintain a state between multiple screens. Here comes the part of State Management. There are different state management techniques in Flutter like GetX, Provider,
6 min read