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 Link a Custom React Component <MyButton> to Another Page ?
Next article icon

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

Last Updated : 24 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Screenshot-from-2023-10-06-10-46-32

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

Peek-2023-10-06-11-11

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

ezgifcomgifmaker

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; 

OutputPeek-2023-10-06-12-07

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.


Next Article
How to Link a Custom React Component <MyButton> to Another Page ?

A

archnabhardwaj
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • React-Questions
  • ReactJS-Basics

Similar Reads

  • How to Pass Data From Child Component To Its Parent In ReactJS ?
    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 co
    6 min read
  • How to Pass a React Component into Another to Transclude Content?
    Transclusion refers to Passing a react component's content to another component. Passing component to another in react enable use to render the data inside other component. This content can be rendered as component's children. ApproachTo pass a react component to another in react we will pass it as
    2 min read
  • How to Link a Custom React Component <MyButton> to Another Page ?
    React is a powerful JavaScript library for building user interfaces, and one of its key features is the ability to create reusable components. In this article, we will walk you through the process of linking a custom React component, <MyButton>, to another page using react-router-dom. Prerequi
    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 Pass Value from One Child Component to Another in VueJS ?
    Vue.js is a JavaScript framework used in building powerful and beautiful user interfaces. The key feature of Vue.js is its component-based architecture that allows the developers to create reusable and modular components. In this article, we will learn how to pass value from one child component to a
    3 min read
  • 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 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 communicate from parent component to the child component in Angular 9 ?
    Angular makes the communication between components very easy. In this article, we will learn how to communicate from a parent component to the child component. Approach: Let's create two components: parent child In the parent component, declare the property that you want to receive in the child comp
    2 min read
  • How to embed two components in one component ?
    React allows us to render one component inside another component. It means we can create the parent-child relationship between the 2 or more components. In the ReactJS Components, it is easy to build a complex UI of any application. We can divide the UI of the application into small components and r
    2 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