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>
  );
};
Answer from Rodrigo Amaral on Stack Overflow
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;

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);
 }
Discussions

How can I send data from a child to a parent functional component in ReactJS?
I am trying to build a website to convert recipes to a different number of servings. I am using React. I have setup a component to take the number of original servings, new number of servings, and ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
reactjs - pass data from child to parent in functional components - Stack Overflow
for passing data from parent to child we can pass it as props like : but i would like to send data from child to parent, note that am using functional components, i trie... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
Using callback function to pass data from child to parent component
Create the state in the parent and pass the setter and getter in the child as props. When you set state in the child you will have the state in the parent More on reddit.com
๐ŸŒ r/react
4
8
January 1, 2024
๐ŸŒ
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...
๐ŸŒ
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 - In conclusion, passing data up from child components to parent components in React can be achieved by defining a callback function in the parent component and passing it down to the child component as a prop.
๐ŸŒ
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.
๐ŸŒ
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 - Weโ€™ve successfully passed Data from the Child component to the Parent component by a callback function. In conclusion, the parent passes Data to the child by setting and passing props, and the child sends back Data to the parent by a callback ...
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ passing-data-react-between-parent-child-functional-components-tay
Passing data in React between Parent and Child in Functional Components
November 17, 2022 - I struggle this when I first started out. There are two ways of passing data in React components ... When you are passing data from, you can use props to transfer the data. The Child will access the data in the props.
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 - Sometimes you need a more direct way to interact with child components. React's useRef and forwardRef provide this capability: // Parent Component function ParentComponent() { const childRef = useRef(); const handleParentButtonClick = () => { // Directly call child's method childRef.current.childMethod(); }; return ( <div> <button onClick={handleParentButtonClick}> Call Child Method </button> <ChildComponent ref={childRef} /> </div> ); } // Child Component const ChildComponent = forwardRef((props, ref) => { const childMethod = () => { console.log('Method called from parent!'); }; // Expose the method to parent useImperativeHandle(ref, () => ({ childMethod })); return <div>Child Component</div>; });
๐ŸŒ
React
react.dev โ€บ learn โ€บ passing-props-to-a-component
Passing Props to a Component โ€“ React
You will often use the children prop for visual wrappers: panels, grids, etc. ... The Clock component below receives two props from its parent component: color and time. (The parent componentโ€™s code is omitted because it uses state, which we wonโ€™t dive into just yet.) ... export default function Clock({ color, time }) { return ( <h1 style={{ color: color }}> {time} </h1> ); }
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ 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
April 4, 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.
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ react-pass-data-from-child-to-parent
Passing data from child to parent component in ReactJS | bobbyhadz
April 7, 2024 - We passed the function as a prop to the Child component, so the Child can call the function and pass it any data we need to access in the Parent.
๐ŸŒ
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 - One of the simplest ways to pass data from a parent component to a child component in React is by using props. Props are properties that are passed down from a parent component to a child component.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ pass-data-between-components-in-react
How to Pass Data and Events Between Components in React
June 8, 2021 - Case 1: If you are using a functional component, simply catch the parentToChild in the parameters. import React from 'react' export default function Child({parentToChild}) { return ( <div> {parentToChild} </div> ) }
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to Pass Data from Child to Parent Component in React? - YouTube
To pass data from a child to a parent component in React:1: Pass a function as a prop to the Child component.2: Call the function in the Child component and ...
Published ย  February 1, 2023
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-pass-property-from-a-parent-component-props-to-a-child-component
How To Pass Props From Parent to Child Component in ReactJS? | GeeksforGeeks
October 13, 2024 - ... Embed the child component to the parent component. Pass the props to the child component as an argument while embedding it to the parent component. In the child component, access the data variable value by writing the name or variable only.
๐ŸŒ
Alma Better
almabetter.com โ€บ bytes โ€บ tutorials โ€บ reactjs โ€บ passing-data-between-reactjs-components
Passing Data Between React JS Components Using Props
October 18, 2023 - In this example, we passed the string "Hello from Parent!" as the **message** prop to the **Child** component. Inside the **Child** component, we accessed the **message** prop using the **props** parameter passed to the function.
Top answer
1 of 3
1

It is anti-pattern in React to store passed props into local state, so you don't really need the numOriginalServings state in PreInputs. Pass originalServings to PreInputs to be used as the default value of the input and pass the updated input value back to the parent component.

PreInputs

const PreInputs = (props) => {
  const handleOriginalServingsChange = (e) => {
    const originalServings = e.target.value;
    props.changeOriginalServings(originalServings);
  };

  return (
    <div className="pre-inputs">
      <form>
        <label>Original Servings: </label>
        <input
          defaultValue={props.originalServings} // <-- initialize input
          onChange={handleOriginalServingsChange}
          type="number"
        />
      </form>
      <h1>Placeholder</h1>
    </div>
  );
};

Parent

function App() {
  //define variables using states
  const [numIngredients, setNumIngredients] = useState(5);
  const [originalServings, setOriginalServings] = useState(8);
  const [newServings, setNewServings] = useState(4);

  //create handling functions for the variables
  const handleNumIngredients = (num) => {
    setNumIngredients(num);
  };

  const handleOriginalServings = (num) => {
    setOriginalServings(num);
  };

  const handleNewServings = (num) => {
    setNewServings(num);
  };

  return (
    <div>
      <Header title="test"/>
      <h1>{originalServings}</h1>
      <PreInputs
        originalServings={originalServings} // <-- pass state
        changeOriginalServings={handleOriginalServings} // <-- pass callback
      />
    </div>
  );
}

2 of 3
0

if you want to send data to the parent component

you have 3 ways for it

1- use redux

2- use context

3- make function in the parent component and get data in this function and sent function to the child component
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 63384062 โ€บ pass-data-from-child-to-parent-in-functional-components
reactjs - pass data from child to parent in functional components - Stack Overflow
for passing data from parent to child we can pass it as props like : but i would like to send data from child to parent, note that am using functional components, i trie...
๐ŸŒ
Webtips
webtips.dev โ€บ solutions โ€บ pass-data-from-child-to-parent-in-react
How to Easily Pass Data From Child to Parent in React - Webtips
September 13, 2025 - ... const Child = ({ callback }) ... the callback inside the Parent const handleCallback = () => callback(state) return ( <button onClick={handleCallback}>Pass data to parent</button> ) }...