score:0

Accepted answer

you probably have a typo in class childcomp extends parentcomp. apart from it, you can simply access the parent function in child using this keyword as if it is its own function.

class parentcomp extends react.component {
  constructor(props) {
    super(props);
  }

  foo() {
    console.log("hello world");
  }

  componentdidmount() {
    this.foo();
  }
}

class childcomp extends parentcomp {
  componentdidmount() {
    this.foo();
  }
  render() {
    return "hello codesandbox";
  }
}

demo codesandbox

score:0

pass the parent method to the child as a property.

export default class parentcomp extends grandparentcomp {
    constructor(props) {
      super(props);
    }

    foo() {
      console.log("hello world")
    }

    render() {
      return (
        <childcomp foo={this.foo()}
        );
    }
  }

call it in the child component.

export default class childcomp extends parentcomp {
  constructor(props){
    super(props)
  }


  componentwillmount() {
    this.props.foo();
  }
}

Related Query

More Query from same tag