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

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

    super() is used to call the parent constructor.

    super(props) would pass props to the parent constructor.

    From your example, super(props) would call the React.Component constructor passing in props as the argument.

    More information on super: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super

提交回复
热议问题