1

I have two components:

  1. Parent component
  2. Child A component
  3. Child B component

I am unable to understand how to do it. Have gone through Call child component method from parent in react , Call child method from parent and How to call `this.props.children` 's function from parent component? but able to comprehend how to do it.

class Parent extends Component {
  render() {
    return (
      <>
        <button onClick={getAlert()}>Click</button>
        <ChildA />
      <>
      );
    }
  }
class ChildA extends Component {
 
  render() {
    return (
      <ChildB />
    );
  }
}
class ChildB extends Component {
  getAlert() {
    alert('clicked');
  }
 
  render() {
    return (
      <h1>Hello</h1>
    );
  }
}

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

  • This is not the best comment you'll read but it's not how it should be done in react. It should be the other way around, every component should handle the logic of itself and its children not its parents. – Alaa Eddine Cherif Aug 01 '22 at 14:51
  • There is a way to do it but it uses refs and stuff that i'm too easily distracted to read. Check this https://stackoverflow.com/questions/37949981/call-child-method-from-parent – Alaa Eddine Cherif Aug 01 '22 at 14:52

0 Answers0