How to override a parent class method in React?

后端 未结 4 567
一个人的身影
一个人的身影 2021-01-05 03:07

I\'m extending a base class and overriding a method in the base class. But when I call it, it calls the super class version. How do I override the method?

           


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-05 03:35

    I found the answer (adapted from here: https://gist.github.com/Zodiase/af44115098b20d69c531 ) - the base class needs to also be defined in an ES6 manner:

    class Hello extends React.Component {
    
            //abstract getName()
            getName()
            {
                if (new.target === Hello) {
                    throw new TypeError("method not implemented");
                }
            }
    
            render() {
    
                return 
    This is: {this.getName()}
    ; } };

提交回复
热议问题