Rohullah Rajaee Rad
Rohullah Rajaee Rad

Reputation: 581

ReactJS: Trigger event from child in parent component

i have two components: 1.Parent 2.Child there is an event in child component called onChange() which return a value. i want to receive the value which was returned from OnChange() in componentDidMount() in parent component.

Example:

class Parent extends PureComponent {
componentDidMount() {
 let value = CHILD.onChange(); //triggered when ever onChange() 
}
 render(){
      return(
        <Child />
)
  }
}
const Child = () => {

  const onChange = () => {
    const value = 1
    return value;
  };
}

Upvotes: 0

Views: 84

Answers (1)

Oner T.
Oner T.

Reputation: 413

class Parent extends PureComponent {


handleChildChange = value => {
 //Do your stuff with value, pass it to the state and take it from there if you like
}


 render(){
      return(
        <Child handleChange={this.handleChildChange} />
)
  }
}
const Child = (props) => {

  const onChange = () => {
    value = 1
    props.handleChange(value);
    }
  };
}

Upvotes: 1

Related Questions