get value from child component 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: how to get a value into a react component

class NameForm extends React.Component {
  constructor(props) {
    super(props);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.input = React.createRef();
  }

  handleSubmit(event) {
    alert('A name was submitted: ' + this.input.current.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Name:
          <input type="text" ref={this.input} />
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}