Call child component method from parent component in reactjs

后端 未结 5 901
[愿得一人]
[愿得一人] 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条回答
  •  旧时难觅i
    2021-02-10 02:20

    You can assign a ref to the child component and then call the function form parent like

    class Parent extends React.Component {
       callChildFunction = () => {
           this.child.handleActionParent();  ///calling a child function here
      } 
       render(){
          return (
               
    {/* other things */} this.child = cd}/>
    ) } } class Child extends React.Component { handleActionParent = () => { console.log('called from parent') } render() { return ( {/*...*/} ) } }

提交回复
热议问题