Why do we need to call super(props) in constructor function in React.js?

How to use states in React.js »

class App extends React.Component {

constructor(props) {
    super(props);
}

...

}

In the above code, you have extended React.Component to App component. Do note that that React.Component also has its own constructor() method and a set of initialization. However, when you create a constructor method inside App component, it’ll override the properties of React.Component. To avoid that overriding, you call the super(props) method. This method basically ensures that all the properties defined inside the constructor method of React.Component component is preserved when you add your own custom constructor method. super(props) is nothing but a reference to the parent class’s constructor function.

Leave a Comment