Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
How to Pass Data from One Component to Another Component in ReactJS?
Next article icon

How to Pass Data From Child Component To Its Parent In ReactJS ?

Last Updated : 04 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  1. 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.
  2. 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.
  3. 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 Structure

Step 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.


Next Article
How to Pass Data from One Component to Another Component in ReactJS?

M

manikumarsingh789
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • React-Questions

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
    4 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 Angular Using 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 t
    3 min read
  • How to Update Parent Data from Child Component in VueJS?
    In Vue.js, communication between components is essential for building dynamic applications. One common scenario is updating the parent component's data from a child component. we will cover several ways to achieve this, including custom events, and v-model. Updating parent data from a child componen
    3 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.
    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 parse JSON Data into React Table Component ?
    In React parsing JSON Data into React Table Component is a common task to represent and structure data. We can render the JSON data into a table dynamically using the array map. Prerequisites:NPM & Node JSReact JSJSON parse()ApproachTo render JSON data in React Table we will be using the JavaScr
    2 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 app Prerequisites:React JSNodejs and npmJavaScript Array mapApproachTo m
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences