How to Pass Data From Child Component To Its Parent In ReactJS ?
Last Updated :
04 Apr, 2025
In ReactJS, the flow of data is typically one-way, meaning data is passed from parent to child components using props. However, there are situations where you may need to pass data from a child component back up to its parent component.
In this article, we will cover how to pass data from a child component to its parent in ReactJS.
Why Do We Need to Pass Data From Child to Parent?
Passing data from a child to a parent is necessary in situations where the parent needs to update its state or trigger certain actions based on data from a child. Common scenarios include:
- State Updates: When a child component needs to notify the parent of a state change (e.g., a user input).
- Event Handling: When a child component triggers an action in the parent, such as form submission or a button click.
- Shared States: For managing common state across multiple components or synchronizing data between them.
Key Points for Passing Data from Child to Parent in ReactJS
Before diving into the implementation, let’s understand the basic principles:
- Props are Unidirectional: In ReactJS, data flows from parent to child through props. However, props can’t be used to pass data from child to parent directly.
- Callback Functions: To pass data from a child component to its parent, a callback function can be passed from the parent to the child as a prop.
- State Management: The callback function updates the state in the parent component, allowing it to react to changes in the child component.
How to Pass Data From Child to Parent in ReactJS
Follow these steps to pass data from a child to a parent component using callback functions.
Step 1: Create a React application using the following command:
npx create-react-app foldername
cd foldername
Project Structure
Folder StructureStep 2: Create a Callback Function in Parent
In the parent component, define a function that will handle the data from the child. This function will be passed as a prop to the child component.
JavaScript
// Filename - App.js
import React from "react";
import Child from "./Child";
class App extends React.Component {
state = {
name: "",
};
// Callback function to handle data received from the
//child component
handleCallback = (childData) => {
// Update the name in the component's state
this.setState({ name: childData });
};
render() {
const { name } = this.state;
return (
<div>
<Child
parentCallback={this.handleCallback}
/>
{name}
</div>
);
}
}
export default App;
In this code :
- The parent component (App) defines a handleCallback function that updates the state (name) when the child sends data.
- The parentCallback function is passed as a prop to the child component (Child), allowing the child to call it and send data back to the parent.
- The updated name is then displayed in the parent component.
Step 3: Pass the Function to the Child
The parent component will pass the callback function to the child component as a prop. The child can then invoke this function when necessary.
JavaScript
// Filename - component/Child.js
import React from "react";
class Child extends React.Component {
// Function triggered when the form is submitted
onTrigger = (event) => {
// Call the parent callback function
this.props.parentCallback(
event.target.myname.value
);
event.preventDefault();
};
render() {
return (
<div>
<form onSubmit={this.onTrigger}>
<input
type="text"
name="myname"
placeholder="Enter Name"
/>
<br></br>
<br></br>
<input type="submit" value="Submit" />
<br></br>
<br></br>
</form>
</div>
);
}
}
export default Child;
Output:
Pass Data from Child Component to its Parent in ReactJS
In this code:
- The onTrigger function is triggered when the form is submitted.
- It calls the parentCallback function (passed from the parent component via props) and sends the entered name (event.target.myname.value) to the parent.
- The form prevents the default submission behavior (event.preventDefault()), allowing the data to be passed without reloading the page.
Advantages of Passing Data from Child to Parent
- State Management: This approach allows the parent to maintain the state and share it with other components.
- Event Handling: It allows the child component to trigger events in the parent, which is useful for handling actions or logic that needs to happen in the parent.
- Reusable Child Components: By passing data back to the parent, child components can be made more reusable and independent of the parent’s state.
Common Use Cases
- Form Handling: Sending form data from child to parent for submission or validation.
- User Interaction: Updating the parent’s UI based on user actions in the child (e.g., when a button is clicked or a selection is made).
- Component Communication: Communicating between sibling components through the parent.
Conclusion
Passing data from a child to a parent in ReactJS is done through callback functions. The parent defines a function and passes it as a prop to the child, allowing the child to send data back to the parent. This approach is useful for state management, handling events, and improving component reusability.
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
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
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
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
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read