React, why use super(props) inside of ES6 class constructor? [duplicate]

℡╲_俬逩灬. 提交于 2019-12-17 22:21:58

问题


I realize the super keyword can be used to call functions in a parent component. However, I'm not totally clear why you would use the super keyword in the example below - just passing it whatever props are being passed to the constructor.

Can someone please shed some light on the various reasons for using the super keyword in an ES6 class constructor, in react?

  constructor(props) {
    super(props);

    this.state = {
      course: Object.assign({}, this.props.course),
      errors: {   }
    };

    this.updateCourseState = this.updateCourseState.bind(this);
  }

回答1:


super allows you to access the constructor method of the parent class. The only reason to include props is to access this.props inside of your constructor.

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



来源:https://stackoverflow.com/questions/37891223/react-why-use-superprops-inside-of-es6-class-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!