14

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. Thanks.

2
  • Possible duplicate of Call child method from parent Commented Sep 3, 2019 at 7:23
  • Although I'm very late to this, I'm also learning React so I'm curious about the case in which a parent would need to call child method. Would you care to explain? Commented Nov 26, 2019 at 8:20

5 Answers 5

8

If using React Hooks, you can make use of useRef and useImperativeHandle hooks.

In the child component, add the functions in the hook.

const Child = forwardRef((props, ref) => {

  const printSomething = () =>{
     console.log('printing from child function')
  }
  useImperativeHandle(ref, () => ({
    printSomething: printSomething
  }));

  return <h1>Child Component</h1>;
});

Call the exposed function from the parent with the ref.

const Parent = () => {
  const childRef = useRef();

  return (
    <div>
      <Child ref={childRef} />
      <button onClick={() => childRef.current.printSomething()}>Click</button>
    </div>
  );
};
Sign up to request clarification or add additional context in comments.

Comments

3

Don't :)

Hoist the function to the parent and pass data down as props. You can pass the same function down, in case the child needs to call it also.

https://facebook.github.io/react/docs/lifting-state-up.html

1 Comment

There are use cases when a parent component wants to trigger an action in a child component, like to trigger its focus. A state variable change on the parent could do that, but it is not practical. You would have to make a counter of something like that and it does not look like a good implementation. The React forwardRef hook specifically mentions this focus use case in its official docs here: react.dev/reference/react/forwardRef#usage
3

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 (
           <div>
           {/* other things */}
           <Child ref={(cd) => this.child = cd}/>
           </div>
      )
    }
}

class Child extends React.Component {
   handleActionParent = () => {
       console.log('called from parent')
   }
   render() {
      return (
           {/*...*/}
      )
   }
}

Comments

1

You can pass registerCallback props to your child and call it from componentDidMount and pass reference to your child component method, then you can call it method at anytime

Comments

1

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 (
            <div>
                <Messages ref={this.child} comment={this.state.comment}/>
            </div>
        )
    }

and in the Message(child) component

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

  };

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.