Call child method from parent
Asked 07 September, 2021
Viewed 2.7K times
  • 62
Votes

I have two components:

  1. Parent component
  2. Child component

I was trying to call Child's method from Parent, I tried this way but couldn't get a result:

class Parent extends Component {
  render() {
    return (
      <Child>
        <button onClick={Child.getAlert()}>Click</button>
      </Child>
      );
    }
  }

class Child extends Component {
  getAlert() {
    alert('clicked');
  }
 
  render() {
    return (
      <h1 ref="hello">Hello</h1>
    );
  }
}

Is there a way to call Child's method from Parent?

Note: Child and Parent components are in two different files.

20 Answer