Call child component method from parent component in reactjs

后端 未结 5 886
[愿得一人]
[愿得一人] 2021-02-10 01:41

I have requirement to call child component method from parent component in reactjs. I have tried using refs but not able to do it. Can anyone please suggest any solution. Than

5条回答
  •  感情败类
    2021-02-10 02:22

    in your parent you can create a reference

    in the constructor:

     this.child = React.createRef();
    

    in any function:

    execute=(comment)=>{
            this.child.current.addComment();
        }
    
    render(){
            return (
                
    ) }

    and in the Message(child) component

    addComment=()=>{
        console.log("Pi ", this.props);
    
      };
    

提交回复
热议问题