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

malexanders picture malexanders · Jun 18, 2016 · Viewed 23.2k times · Source

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);
  }

Answer

jmancherje picture jmancherje · Jun 18, 2016

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?