0

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;
  };
}
2
  • 1
    You need to define the onChange() method in your Parent and then pass it as a prop in your Child component and execute it there. The value can be saved in the state of the Parent component. Commented Dec 25, 2019 at 9:41
  • 1
    You should define the onChange handler on the Parent component and pass over it to the Child component as a prop. In the child component, you receive the handler and set it to the appropriate event. Commented Dec 25, 2019 at 9:42

1 Answer 1

1
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);
    }
  };
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.