almost 4 years ago

## React ES6 class constructor super()

When writting React using the ES6 class syntax like this:

Two questions may arise:

1. Is it necessary to call super() inside constructor?
2. What is the difference between callling super() and super(props)?

Always call super() if you have a constructor and don't worry about it if you don't have a constructor

Calling super() is necessary only if you need to have a constructor. Take a look at this code:

The code above is perfectly valid. You don't have to call super() for every react component you create. However, if there is a constructor in your code, then you MUST call super:

The reason why this cannot be allowed before super() is because this is uninitialized if super() is not called [1]

You may think you can get away with an empty constructor without callling super():

ES6 class constructors MUST call super if they are subclasses. Thus, you have to call super() as long as you have a constructor. (But a subclass does not have to have a constructor)

Call super(props) only if you want to access this.props inside the constructor. React automatically set it for you if you want to access it anywhere else.
The effect of passing props when calling super() allows you to access this.props in the constructor:
There is no need to pass props into the constructor if you want to use it in other places. Because React automatically set it for you [2]