Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the call to super(props) in an ES6 class important?

Suppose I've the following class:

class Tabs extends React.Component {
  displayName: Tabs;

  static propTypes = {
  selected: React.PropTypes.number,
  children: React.PropTypes.oneOfType([
    React.PropTypes.array,
    React.PropTypes.element
  ]).isRequired
  };

  constructor() {
  super();
  this.state = {
    selected: 0,
    maxSelected: 0
  };

  render() {
    return(
      <div>
       {this.props.selected}
       {this.props.children}
      </div>
    );
  }
};

I want to know that if passing the following constructor is important:

constructor(props) {
  super(props);
}

My current code works just fine but I wanted to know if this is a good practice.

like image 609
Nirmalya Ghosh Avatar asked Mar 07 '16 02:03

Nirmalya Ghosh


1 Answers

According to Sophie Alpert with the React team it's only necessary to pass props into the constructor if you intend on using this.props inside the constructor. After the constructor is invoked, React attaches the props to the component from the outside.

like image 190
Rick Runyon Avatar answered Oct 16 '22 16:10

Rick Runyon