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
How To Pass Props From Parent to Child Component in ReactJS? ReactJS is a powerful library that helps developers build interactive user interfaces by breaking them into reusable components. One of the essential features of React is the ability to pass data between components using props. In React, props allow parent components to send data or functions to chi
3 min read
How to pass data from Parent to Child component in Angular ? We can use the @Input directive for passing the data from the Parent to Child component in AngularUsing Input Binding: @Input - We can use this directive inside the child component to access the data sent by the parent component. Here app.component is the Parent component and cdetail.component is th
2 min read
How to set Parent State from Children Component in ReactJS? To set the parent state from a child component, we use Reactâs unidirectional data flow. Instead of directly passing data, we pass a function that enables the child to send data back to set the parent state.Prerequisites:React JSuseState HookApproachTo set parent state from child component in React,
2 min read
How to Pass Data from One Component to Another Component in ReactJS? 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.
5 min read
Passing data from Child to Parent Component in Angular In Angular, passing data from a child component to its parent component involves emitting events. Essentially, the child component emits an event containing the data that the parent component needs to receive. This is typically achieved using Angular's EventEmitter class, where the child component e
3 min read
How to pass data into table from a form using React Components ? React JS is a front-end library used to build UI components. This article will help to learn to pass data into a table from a form using React Components. This will be done using two React components named Table and Form. We will enter data into a form, which will be displayed in the table on 'submi
3 min read
How to Call Parent Components's Function from Child Component in React ? In React, it is very important to manage communication between components for building flexible and maintainable applications. Sometime, you need to trigger a function defined in a parent component from a child component. In this article, we'll see how to achieve this in React by passing down functi
3 min read
How to Map Data into Components using ReactJS? Mapping data in react js component is a comman practice to render the lists and repeating elements. In this article, we will have a users data and we will map it to react components to display the users information in the react appPrerequisites:React JSNodejs and npmJavaScript Array mapApproachTo ma
3 min read
How to get text inside Text component onPress in ReactJS ? In this article, we will learn "How to get the text inside Text component onPress in ReactJS?". Problem Statement: Sometimes in an application, it is required to get the text inside certain components, in order to reuse the components again & again just by changing the text value. Approach: We w
4 min read