How to Pass Data from One Component to Another Component in ReactJS?
Last Updated :
24 Mar, 2025
In ReactJS, components are the building blocks of your user interface. Components allow you to create modular, reusable UI elements, but sometimes these components need to communicate with each other.
In this article, we will explore the different methods to pass data between components in ReactJS.
1. Passing data from Parent to Child in React
The most common way to pass data between components is through props (short for properties). Props are read-only and allow you to pass information from a parent component to its child components. When you define a child component, you can pass it data by setting its props.
First, you need to create a React App - Create a New React App
Folder Structure

CSS
/* Filename - App.css*/
.App {
text-align: center;
}
.geeks {
color: green;
}
JavaScript
// Filename - App.js
import React from "react";
import "./index.css";
import Parent from "./Parent";
import "./App.css";
const App = () => {
return (
<div className="App">
<h1 className="geeks">Geeksforgeeks</h1>
<h3>This is App.js Component</h3>
<Parent />
</div>
);
};
export default App;
JavaScript
// Filename - Parent.js
import React from "react";
import Child from "./Child";
const Parent = () => {
const data = "Data from Parent to Child";
return (
<div>
<h4>This is Parent component</h4>
<Child data={data} />
</div>
);
};
export default Parent;
JavaScript
// Filename - Child.js
import React from "react";
const Child = (props) => {
return <h3> {props.data} </h3>;
};
export default Child;
Step to Run Application: Run the application using the following command from the root directory of the project
npm start
Output

In this code
- The App component renders the Parent component.
- The Parent component passes a string ("Data from Parent to Child") as a prop (data) to the Child component.
- The Child component displays this data passed down from the Parent.
2. Passing data from Child to Parent Component
In React, data usually flows from parent to child through props. But what if you need to pass data from a child component back to the parent? To achieve this, you can pass a callback function from the parent to the child. When the child component needs to send data to the parent, it calls this function with the data.
CSS
/* Filename - App.css */
.App {
text-align: center;
}
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
}
.item {
min-width: 33rem;
text-align: left;
}
.geeks {
color: green;
}
JavaScript
// Filename - App.js
import React from "react";
import "./index.css";
import Parent from "./Parent";
import "./App.css";
const App = () => {
return (
<div className="App">
<h1 className="geeks">GeeksforGeeks</h1>
<Parent />
</div>
);
};
export default App;
JavaScript
// Filename - Parent.js
import React from "react";
import Child from "./Child";
class Parent extends React.Component {
state = {
msg: "",
};
handleCallback = (childData) => {
this.setState({ msg: childData });
};
render() {
const { msg } = this.state;
return (
<div>
<Child
parentCallback={this.handleCallback}
/>
<h1> {msg}</h1>
</div>
);
}
}
export default Parent;
JavaScript
// Filename - Child.js
import React from "react";
class Child extends React.Component {
onTrigger = () => {
this.props.parentCallback("Welcome to GFG");
};
render() {
return (
<div>
<br></br> <br></br>
<button onClick={this.onTrigger}>
Click me
</button>
</div>
);
}
}
export default Child;
Output

In this code
- App.js renders the Parent component.
- Parent.js defines a state (msg) and a handleCallback method to update the state when the child sends data.
- Child.js has a button, and when clicked, it calls the parentCallback function (passed from the parent) to send the message "Welcome to GFG" back to the parent.
- The parent updates its state with the data and displays it in an <h1> tag.
3. Passing Data Between Sibling Components
When you need to pass data between sibling components (components that share the same parent), the best way is to use their common parent component as a mediator. The parent component can manage the shared state and pass it down to both child components.
For passing data among siblings, there are multiple methods we can choose from as shown below
- Combination of the above two methods (callback and use of props).
- Using Redux.
- ContextAPI
Folder Structure

CSS
/* Filename - App.css */
.App {
text-align: center;
}
.geeks {
color: green;
}
JavaScript
// Filename - App.js
import { React, useState, createContext } from "react";
import "./index.css";
import Child1 from "./Child1";
import "./App.css";
import Child2 from "./Child2";
// Create a new context and export
export const NameContext = createContext();
// Create a Context Provider
const NameContextProvider = ({ children }) => {
const [name, setName] = useState(undefined);
return (
<NameContext.Provider value={{ name, setName }}>
{children}
</NameContext.Provider>
);
};
const App = () => {
return (
<div className="App">
<h1 className="geeks">GeeksforGeeks</h1>
<NameContextProvider>
<Child1 />
<Child2 />
</NameContextProvider>
</div>
);
};
export default App;
JavaScript
// Filename - Child1.js
import React, { useContext } from "react";
import { NameContext } from "./App";
const Child1 = () => {
const { setName } = useContext(NameContext);
function handle() {
setName("Geeks");
}
return (
<div>
<h3>This is Child1 Component</h3>
<button onClick={() => handle()}>Click </button>
</div>
);
};
export default Child1;
JavaScript
// Filename - Child2.js
import React, { useContext } from "react";
import { NameContext } from "./App";
const Child2 = () => {
const { name } = useContext(NameContext);
return (
<div>
<br />
<h4>This is Child2 Component</h4>
<h4>hello: {name}</h4>
</div>
);
};
export default Child2;
Output
In this code
- App.js provides a shared name state via NameContext.
- Child1.js updates the name state to "Geeks" when a button is clicked.
- Child2.js reads and displays the name state.
Conclusion
By following these methods you can easily pass data from parent to child, child to parent and also able to pass data between the siblings.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read