What's the difference between “super()” and “super(props)” in React when using es6 classes?

后端 未结 10 1947
不思量自难忘°
不思量自难忘° 2020-11-22 09:01

When is it important to pass props to super(), and why?

class MyComponent extends React.Component {
  constructor(props) {
    supe         


        
10条回答
  •  隐瞒了意图╮
    2020-11-22 09:44

    Dan Abramov wrote an article on this topic:

    Why Do We Write super(props)?

    And the gist of it is that it's helpful to have a habit of passing it to avoid this scenario, that honestly, I don't see it unlikely to happen:

    // Inside React
    class Component {
      constructor(props) {
        this.props = props;
        // ...
      }
    }
    
    // Inside your code
    class Button extends React.Component {
      constructor(props) {
        super(); // 

提交回复
热议问题