how to send data from child component to parent component in react code example

Example 1: pass data from child component to parent component react native

class Parent extends React.Component {
  state = { message: "" }
  callbackFunction = (childData) => {
        this.setState({message: childData})
  },
  render() {
          return (
              <div>
                   <Child1 parentCallback = {this.callbackFunction}/>
                   <p> {this.state.message} </p>
              </div>
          );
  }
}

class Child1 extends React.Component{
  sendData = () => {
           this.props.parentCallback("Hey Popsie, How’s it going?");
      },
  render() { 
  //you can call function sendData whenever you'd like to send data from child component to Parent component.
      }
};

Example 2: pass element from child to parent react

Parent:

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

Child:

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

Example 3: how to pass props from child to parent

class ToDoList extends React.Component {    constructor(props) {        super(props);        this.state = {            listDataFromChild: null        };        },    myCallback = (dataFromChild) => {        this.setState({ listDataFromChild: dataFromChild });    },    otherFn = () => {        [...within this other function now I still have access to this.state.listDataFromChild...]    }    render() {        return (            <div>                 <ToDoItem callbackFromParent={this.myCallback}/>                 [...now here I can pass this.state.listDataFromChild as a prop to any other child component...]                   </div>        );    }});

Example 4: how to pass state from parent to child in react

class SomeParentComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {color: 'red'};
  }
  render() {
    return <SomeChildComponent color={this.state.color} />;
  }
}