最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to override a parent class method in React? - Stack Overflow

programmeradmin3浏览0评论

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?

    var Hello = React.createClass( {

        getName: function() { return "super" },

        render: function() {

            return <div>This is: {this.getName()}</div>;
        }
    });

    class HelloChild extends Hello {
        constructor(props) {
          super(props);

          console.log( this.getName());
        }
        getName()
        {
          return "Child";
        }
    };

I want it to print "This is: Child" but it prints "This is: super"

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?

    var Hello = React.createClass( {

        getName: function() { return "super" },

        render: function() {

            return <div>This is: {this.getName()}</div>;
        }
    });

    class HelloChild extends Hello {
        constructor(props) {
          super(props);

          console.log( this.getName());
        }
        getName()
        {
          return "Child";
        }
    };

I want it to print "This is: Child" but it prints "This is: super"

Share Improve this question edited Aug 4, 2016 at 10:58 caub 2,7692 gold badges30 silver badges32 bronze badges asked Aug 4, 2016 at 4:30 Don RhummyDon Rhummy 25.8k52 gold badges192 silver badges361 bronze badges 1
  • 1 jsbin.com/qizihe/edit?html,js,output – caub Commented Aug 4, 2016 at 11:30
Add a comment  | 

5 Answers 5

Reset to default 5

The problem is that you're mixing ES6 type class declaration (ex. Hello) with old school Javascript declaration (ex. HelloChild). To fix HelloChild, bind the method to the class.

class HelloChild extends Hello {
    constructor(props) {
      super(props);

      this.getName = this.getName.bind(this); // This is important

      console.log( this.getName());
    }

    getName()
    {
      return "Child";
    }
};

Then it'll work.

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 <div>This is: {this.getName()}</div>;
        }
    };

Actually you can override method to execute code from your subclass

class Hello extends React.Component {
getName() {
 super.getName();
 }
}


class HelloChild extends Hello {
getName()
    {
      return "Child";
    }
}

Please note that this answer proposes different approach:

I wonder why you should do this in the first place, my point is that directly coupling two react components is not a right way to implement re-usability in React.

If you are trying to have multiple child components which extends one parent, What I would do is, to have child components and a higher-order component and then implement common functionality with Composition. This way you can skip those methods, which you were trying to override and so everything would stay clear.

If you're reading this far, you've likely found that this still doesn't work correctly:

class Hello extends React.Component {
  getName() {
    if (new.target === Hello) {
      throw new TypeError("method not implemented");
    }
  };

  render() {
    return <div>This is: {this.getName()}</div>;
  }
}

class HelloChild extends Hello {
  getName() {
    return "Child";
  };
}

I used to not think arrow functions on classes were important - it looked like old school javascript, but it's exactly what's needed.

Arrow functions correctly preserve this context, this is the correct way to do it:

class Hello extends React.Component {
  getName = () => {
    if (new.target === Hello) {
      throw new TypeError("method not implemented");
    }
  };

  render() {
    return <div>This is: {this.getName()}</div>;
  }
}

class HelloChild extends Hello {
  getName = () => {
    return "Child";
  };
}
发布评论

评论列表(0)

  1. 暂无评论