This should work. While sending the prop back you are sending that as an object rather send that as a value or alternatively use it as an object in the parent component. Secondly you need to format your json object to contain name value pairs and use valueField and textField attribute of DropdownList

Short Answer

Parent:

<div className="col-sm-9">
     <SelectLanguage onSelectLanguage={this.handleLanguage} /> 
</div>

Child:

handleLangChange = () => {
    var lang = this.dropdown.value;
    this.props.onSelectLanguage(lang);            
}

Detailed:

EDIT:

Considering React.createClass is deprecated from v16.0 onwards, It is better to go ahead and create a React Component by extending React.Component. Passing data from child to parent component with this syntax will look like

Parent

class ParentComponent extends React.Component {

    state = { language: '' }

    handleLanguage = (langValue) => {
        this.setState({language: langValue});
    }

    render() {
         return (
                <div className="col-sm-9">
                    <SelectLanguage onSelectLanguage={this.handleLanguage} /> 
                </div>
        )
     }
}

Child

var json = require("json!../languages.json");
var jsonArray = json.languages;

export class SelectLanguage extends React.Component {
    state = {
            selectedCode: '',
            selectedLanguage: jsonArray[0],
        }

    handleLangChange = () => {
        var lang = this.dropdown.value;
        this.props.onSelectLanguage(lang);            
    }

    render() {
        return (
            <div>
                <DropdownList ref={(ref) => this.dropdown = ref}
                    data={jsonArray} 
                    valueField='lang' textField='lang'
                    caseSensitive={false} 
                    minLength={3}
                    filter='contains'
                    onChange={this.handleLangChange} />
            </div>            
        );
    }
}

Using createClass syntax which the OP used in his answer Parent

const ParentComponent = React.createClass({
    getInitialState() {
        return {
            language: '',
        };
    },

    handleLanguage: function(langValue) {
        this.setState({language: langValue});
    },

    render() {
         return (
                <div className="col-sm-9">
                    <SelectLanguage onSelectLanguage={this.handleLanguage} /> 
                </div>
        );
});

Child

var json = require("json!../languages.json");
var jsonArray = json.languages;

export const SelectLanguage = React.createClass({
    getInitialState: function() {
        return {
            selectedCode: '',
            selectedLanguage: jsonArray[0],
        };
    },

    handleLangChange: function () {
        var lang = this.refs.dropdown.value;
        this.props.onSelectLanguage(lang);            
    },

    render() {

        return (
            <div>
                <DropdownList ref='dropdown'
                    data={jsonArray} 
                    valueField='lang' textField='lang'
                    caseSensitive={false} 
                    minLength={3}
                    filter='contains'
                    onChange={this.handleLangChange} />
            </div>            
        );
    }
});

JSON:

{ 
"languages":[ 

    { 
    "code": "aaa", 
    "lang": "english" 
    }, 
    { 
    "code": "aab", 
    "lang": "Swedish" 
    }, 
  ] 
}
Answer from Shubham Khatri on Stack Overflow
🌐
Medium
medium.com › @ozhanli › passing-data-from-child-to-parent-components-in-react-e347ea60b1bb
Passing Data from Child to Parent Components in React | by ozhanli | Medium
April 11, 2023 - The Child component renders an ... function is called. When the handleClick function is called, it calls the sendDataToParent function passed down from the Parent component with the current value of data....
🌐
DEV Community
dev.to › bcostaaa01 › how-to-pass-props-from-child-to-parent-component-in-react-1ci4
How to pass props from child to parent component in React - DEV Community
September 10, 2024 - Making it simpler to understand ... that we initialise the state in the Parent component, and then pass the variables down to the Child component, and the respective function to update the value of the state in the Parent component...
Discussions

i don't understand how child-to-parent component communication works.
It's quite simple actually. Parent component can pass props down to child component. Child component cannot pass anything up to parent. If you want to show information in child, which is available in parent, you pass it down as a prop. If you want to update that information from child, you pass function that updates it from parent to child as a prop and then you have it available to call it. Edit: basically, it's called uni directional data flow. Child cannot send data up, the only way to change something in parent is to receive updater function as a prop and call it. More on reddit.com
🌐 r/reactjs
18
8
January 14, 2024
reactjs - React Hook : Send data from child to parent component - Stack Overflow
I'm looking for the easiest solution to pass data from a child component to his parent. I've heard about using Context, pass trough properties or update props, but I don't know which one is the best More on stackoverflow.com
🌐 stackoverflow.com
React - Passing data from child to parent
Title says it all, passing data from component A to component B would do in this case but I imagine that’s not possible and that it’s probably easier to just pass the data back to the parent. So I have a parent class component and 2 child controlled components (A and B). I would like to ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
October 27, 2018
[SOLVED] React Hooks: how to pass data from Child up to Parent? - Help - PureScript Language Forum
For simplicity, suppose I have a parentComponent with a label with text “foo” and a childComponent that renders a button clicking which should make parentComponent change “foo” to “bar”. In raw JS it’s done by obtaining a setText via hook call _ /\ setText More on discourse.purescript.org
🌐 discourse.purescript.org
3
0
October 29, 2024
Top answer
1 of 16
418

This should work. While sending the prop back you are sending that as an object rather send that as a value or alternatively use it as an object in the parent component. Secondly you need to format your json object to contain name value pairs and use valueField and textField attribute of DropdownList

Short Answer

Parent:

<div className="col-sm-9">
     <SelectLanguage onSelectLanguage={this.handleLanguage} /> 
</div>

Child:

handleLangChange = () => {
    var lang = this.dropdown.value;
    this.props.onSelectLanguage(lang);            
}

Detailed:

EDIT:

Considering React.createClass is deprecated from v16.0 onwards, It is better to go ahead and create a React Component by extending React.Component. Passing data from child to parent component with this syntax will look like

Parent

class ParentComponent extends React.Component {

    state = { language: '' }

    handleLanguage = (langValue) => {
        this.setState({language: langValue});
    }

    render() {
         return (
                <div className="col-sm-9">
                    <SelectLanguage onSelectLanguage={this.handleLanguage} /> 
                </div>
        )
     }
}

Child

var json = require("json!../languages.json");
var jsonArray = json.languages;

export class SelectLanguage extends React.Component {
    state = {
            selectedCode: '',
            selectedLanguage: jsonArray[0],
        }

    handleLangChange = () => {
        var lang = this.dropdown.value;
        this.props.onSelectLanguage(lang);            
    }

    render() {
        return (
            <div>
                <DropdownList ref={(ref) => this.dropdown = ref}
                    data={jsonArray} 
                    valueField='lang' textField='lang'
                    caseSensitive={false} 
                    minLength={3}
                    filter='contains'
                    onChange={this.handleLangChange} />
            </div>            
        );
    }
}

Using createClass syntax which the OP used in his answer Parent

const ParentComponent = React.createClass({
    getInitialState() {
        return {
            language: '',
        };
    },

    handleLanguage: function(langValue) {
        this.setState({language: langValue});
    },

    render() {
         return (
                <div className="col-sm-9">
                    <SelectLanguage onSelectLanguage={this.handleLanguage} /> 
                </div>
        );
});

Child

var json = require("json!../languages.json");
var jsonArray = json.languages;

export const SelectLanguage = React.createClass({
    getInitialState: function() {
        return {
            selectedCode: '',
            selectedLanguage: jsonArray[0],
        };
    },

    handleLangChange: function () {
        var lang = this.refs.dropdown.value;
        this.props.onSelectLanguage(lang);            
    },

    render() {

        return (
            <div>
                <DropdownList ref='dropdown'
                    data={jsonArray} 
                    valueField='lang' textField='lang'
                    caseSensitive={false} 
                    minLength={3}
                    filter='contains'
                    onChange={this.handleLangChange} />
            </div>            
        );
    }
});

JSON:

{ 
"languages":[ 

    { 
    "code": "aaa", 
    "lang": "english" 
    }, 
    { 
    "code": "aab", 
    "lang": "Swedish" 
    }, 
  ] 
}
2 of 16
156

To pass data from child component to parent component

In Parent Component:

getData(val){
    // do not forget to bind getData in constructor
    console.log(val);
}
render(){
 return(<Child sendData={this.getData}/>);
}

In Child Component:

demoMethod(){
   this.props.sendData(value);
 }
🌐
React
react.dev › learn › sharing-state-between-components
Sharing State Between Components – React
To do it, remove state from both of them, move it to their closest common parent, and then pass it down to them via props. This is known as lifting state up, and it’s one of the most common things you will do writing React code.
🌐
Reddit
reddit.com › r/reactjs › i don't understand how child-to-parent component communication works.
r/reactjs on Reddit: i don't understand how child-to-parent component communication works.
January 14, 2024 -

so I'm learning from Maximilian Schwarzmüller's React - The Complete Guide 2024 (incl. React Router & Redux). and on module 5 lecture 15 he is teaching about child-to-parent component communication.

I am watching lecture for 2 weeks now and I simply don't understand how this works. I understand that the child component is communicating up to the main component (app component) but while he was creating his own event-listner I don't get why he uses function and calls in parameter.

I feel dumb not able to understand this. It is frustrating. please help 🙏. explain me in details

🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › how-to-pass-data-from-child-component-to-its-parent-in-reactjs
How to Pass Data From Child Component To Its Parent In ReactJS ? - GeeksforGeeks
July 23, 2025 - 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.
Find elsewhere
🌐
Wisp CMS
wisp.blog › blog › how-to-pass-data-from-child-to-parent-component-in-react-a-complete-guide
How to Pass Data from Child to Parent Component in React: A Complete Guide - Wisp CMS
December 9, 2024 - When you need to share data between components that aren't directly related (parent-child), or when you want to avoid prop drilling, the Context API provides an elegant solution: // Create a context const DataContext = React.createContext(); // Parent/Provider Component function ParentComponent() { const [sharedData, setSharedData] = useState(''); return ( <DataContext.Provider value={{ sharedData, setSharedData }}> <div> <h2>Shared Data: {sharedData}</h2> <ChildComponent /> </div> </DataContext.Provider> ); } // Child Component function ChildComponent() { const { setSharedData } = useContext(DataContext); return ( <button onClick={() => setSharedData('Updated from child!')}> Update Shared Data </button> ); }
🌐
Medium
medium.com › @jocheattahdavid › interview-questions-e1-lets-pass-data-from-child-to-parent-716cff70dd15
Pass Data from Child to Parent in React | by David Attah | Medium
October 24, 2023 - Pass the callback function in the parent component as a prop to the child component · The child component then calls the parent callback function using props · Let's see this in practice.
Top answer
1 of 7
132

A common technique for these situations is to lift the state up to the first common ancestor of all the components that needs to use the state (i.e. the PageComponent in this case) and pass down the state and state-altering functions to the child components as props.

Example

const { useState } = React;

function PageComponent() {
  const [count, setCount] = useState(0);
  const increment = () => {
    setCount(count + 1)
  }

  return (
    <div className="App">
      <ChildComponent onClick={increment} count={count} />         
      <h2>count {count}</h2>
      (count should be updated from child)
    </div>
  );
}

const ChildComponent = ({ onClick, count }) => {
  return (
    <button onClick={onClick}>
       Click me {count}
    </button>
  )
};

ReactDOM.render(<PageComponent />, document.getElementById("root"));
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>

<div id="root"></div>

2 of 7
95

You can create a method in your parent component, pass it to child component and call it from props every time child's state changes, keeping the state in child component.

    const EnhancedTable = ({ parentCallback }) => {
        const [count, setCount] = useState(0);
        
        return (
            <button onClick={() => {
                const newValue = count + 1;
                setCount(newValue);
                parentCallback(newValue);
            }}>
                 Click me {count}
            </button>
        )
    };

    class PageComponent extends React.Component { 
        callback = (count) => {
            // do something with value in parent component, like save to state
        }

        render() {
            return (
                <div className="App">
                    <EnhancedTable parentCallback={this.callback} />         
                    <h2>count 0</h2>
                    (count should be updated from child)
                </div>
            )
        }
    }
🌐
Medium
dvmhn07.medium.com › passing-data-between-parent-and-child-components-in-react-part-b405116c470e
Passing Data Between Parent and Child Components in React — PART 1 | by Dev Balaji | Medium
April 18, 2023 - It then passes this state variable down to the Child component as a prop. ... Context is a powerful feature in React that allows data to be passed down through a component tree without the need to pass props down manually at every level.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
React - Passing data from child to parent
October 27, 2018 - Title says it all, passing data from component A to component B would do in this case but I imagine that’s not possible and that it’s probably easier to just pass the data back to the parent. So I have a parent class component and 2 child controlled components (A and B). I would like to use in child B the value of a property in child A. class Parent extends component { state = { value: 1, finalValue: null } render() { ...
🌐
Purescript
discourse.purescript.org › help
[SOLVED] React Hooks: how to pass data from Child up to Parent? - Help - PureScript Language Forum
October 29, 2024 - For simplicity, suppose I have a parentComponent with a label with text “foo” and a childComponent that renders a button clicking which should make parentComponent change “foo” to “bar”. In raw JS it’s done by obtaining a setText via hook call _ /\ setText
🌐
React
react.dev › learn › passing-props-to-a-component
Passing Props to a Component – React
Often, it indicates that you should split your components and pass children as JSX. More on that next! ... When you nest content inside a JSX tag, the parent component will receive that content in a prop called children. For example, the Card component below will receive a children prop set to <Avatar /> and render it in a wrapper div: ... import Avatar from './Avatar.js'; function Card({ children }) { return ( <div className="card"> {children} </div> ); } export default function Profile() { return ( <Card> <Avatar size={100} person={{ name: 'Katsuko Saruhashi', imageId: 'YfeOqp2' }} /> </Card> ); }
🌐
React
react.dev › learn › passing-data-deeply-with-context
Passing Data Deeply with Context – React
Before you use context, try passing props or passing JSX as children. In this example, toggling the checkbox changes the imageSize prop passed to each <PlaceImage>. The checkbox state is held in the top-level App component, but each <PlaceImage> needs to be aware of it. Currently, App passes imageSize to List, which passes it to each Place, which passes it to the PlaceImage. Remove the imageSize prop, and instead pass it from the App component directly to PlaceImage.
🌐
Reddit
reddit.com › r/react › using callback function to pass data from child to parent component
r/react on Reddit: Using callback function to pass data from child to parent component
January 1, 2024 -

I’m new to React, but I'm using it for a school project. I'm building a website that uses the Chart.js library to create a line chart. Basically, the actual chart is rendered in a parent component "Graph.js", but the menu/popup used to edit the graph data is stored in a child component "AddMenu.js". In the child component, I've written a function "increment" that increments the specified point of the line chart's dataset when clicked. However, I am now struggling to pass this data from the child component back to the parent, and re-render the parent.

This is my error:

Here is the callback function I've written in the Graph.js parent component:

Here is what my props for the child AddMenu look like:

Here is the increment function I use in the child AddMenu. graphData is an array of seven values imported from Graph.js:

Here is how I call the callback function in the child AddMenu:

I would greatly appreciate any feedback on how to pass back graphData to the Graph parent component. I modify it in the child AddMenu.

If you would like to see my full code for AddMenu and Graph, please let me know and I will attach photos in the comments. Thank you so much!

Top answer
1 of 5
51

Suppose the state drive needs to be present in the Parent and the Child. Then you define it in the parent and create a callback function responsible for changing this state setDrive.

In general, the state must be defined at closest common ancestor between the components that use or change this state and this is called Lifting State Up. In your case, something like this (complete demo):

Parent:

const VehicleList = () => {
  const classes = useStyles();
  const [drive, setDrive] = React.useState(null); // the lifted state

  const sendDataToParent = (index) => { // the callback. Use a better name
    console.log(index);
    setDrive(index);
  };

  return (
    <div className={classes.root}>
      {vehicleData.map((vehicle) => (
        <div
          key={vehicle.name}
          style={{
            width: "20rem",
            border: vehicle.name === drive ? "1px solid red" : null
          }}
        >
          <Vehicle vehicle={vehicle} sendDataToParent={sendDataToParent} />
        </div>
      ))}
    </div>
  );
};

Child:

const Vehicle = ({ vehicle, sendDataToParent }) => {
  const classes = useStyles();
  const [showDriveAction, setShowDriveAction] = React.useState(false);
  const driveURL = React.useState("");

  return (
    <Paper className={classes.root} elevation={3}>
      <Grid container spacing={2}>
        {/* ... */}
        <CardActions>
          <Button
            onClick={() => {
              sendDataToParent(vehicle.name);
            }} //this is where it needs to be passed
            size="small"
            color="secondary"
            startIcon={<FolderOpenIcon />}
            style={{ fontWeight: "bold" }}
          >
          {/* ... */}
        </CardActions>
      </Grid>
    </Paper>
  );
};
2 of 5
7

Passing data from child to parent in functional components React

const Parent = (props) => {
    let onClickFunction = (arg) => alert(arg);
    return <Child update={onClickFunction} />
}

const Child({ update }) {
    return <button onClick={() => update(1)}> Click </button>;
}

export default Parent;

🌐
CRS Info Solutions
crsinfosolutions.com › home › how can you pass props to children components in react?
How Can You Pass Props to Children Components in React?
Salesforce Training
Passing props (properties) to child components is a fundamental concept in React, a popular JavaScript library for building user interfaces. Understanding how to effectively pass data from parent to child components can greatly enhance the modularity and reusability of your code. I have enrolled for Salesforce Admin and development online course at CRS info solutions. It’s really the best training i have ever taken and syllabus is highly professional
Rating: 5 ​
🌐
Quora
quora.com › How-do-you-pass-data-between-React-components
How to pass data between React components - Quora
Props are parameters of execution for a component and can be supplied by a parent component to a child component. The process of passing data from parent to child components is called prop drilling.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Why do callbacks work for passing information from the child to the parent in react?
October 26, 2018 - For example if in my parent class I have a function like getUserInput = (input) => { //does something } And I pass it as props to a child component like <ChildComponent getUserInput={this.getUserInput} /> And th…
🌐
Codeconcisely
codeconcisely.com › posts › react-child-data-parent
Passing Data From Child to Parent in React - Code Concisely
October 12, 2022 - Here’s how the code from above should be modified to pass data from a child to parent component. import React from 'react'; export function Child({ parentFn }) { parentFn('hello, parent'); return <p>Child</p>; } export default function Parent() { function parentFn(childData) { console.log('Data from child:', childData); // hello, parent } return ( <> <p>Parent</p> <Child parentFn={parentFn} /> </> ); }