0

I have 2 components:

1:Parent 2:Child

I want to pass some data from child to parent. in fact, I have a method in the parent component as following:

mymethod= (message) => {
console.warn(message);
}

in the child component I'm using this:

      <Parent ref= { (child) => {this.refs= child; }} />
      this.refs.mymethod(message);

but when I run that code, I get this:

Undefined is not an object (evaluating _this.ref.method)

1 Answer 1

1

What you are doing is wrong. you need to do pass the method from parent to child as prop and call the method in child as and when required.

  export default Parent extends Component{
         mymethod= (message) => {
    console.warn(message);
    }
     render(){
       return (
       <div>
         <Child mymethod={this.mymethod} />
          <Child2 />
         </div>)
       }    
          }

you can access the method in child as this.props.mymethod(message)

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.