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

后端 未结 10 2039
不思量自难忘°
不思量自难忘° 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:19

    When you pass props to super, the props get assigned to this. Take a look at the following scenario:

    constructor(props) {
        super();
        console.log(this.props) //undefined
    }
    

    How ever when you do :

    constructor(props) {
        super(props);
        console.log(this.props) //props will get logged.
    }
    

提交回复
热议问题