pass state data to child component react code example

Example 1: pass setstate to child

//ChildExt component
class ChildExt extends React.Component {
    render() {
        return (<div><button onClick={() => this.props.handleForUpdate('someNewVar')}>Push me</button></div>
        )}
}
//Parent component
class ParentExt extends React.Component {
  constructor(props){
    super(props);
    this.state = {lol: false }
  }

    handleForUpdate(someArg){
            this.setState({lol: true});
      		console.log(someArg);
    }
  //Notice how we don't pass the arguments into the bind.this even though it does take an argument.
    render() {
        return (<ChildExt handleForUpdate={this.handleForUpdate.bind(this)} />)
    }
}

Example 2: 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 3: pass state to child react

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