1

I have a basic knowledge of JS and now exploring react. I know there are lots of answers to what is super(props) and when to use it in react.

But there is yet answers to HOW it works and WHY do we call super(props) in constructor in React?

fahmisan
  • 43
  • 6
  • Possible duplicate of [What's the difference between "super()" and "super(props)" in React when using es6 classes?](https://stackoverflow.com/questions/30571875/whats-the-difference-between-super-and-superprops-in-react-when-using-e) – Shireesha Parampalli Oct 28 '18 at 02:49
  • Welcome to Stackoverflow. This question is very broad and not so well suited to this site. It would be better to first do a tutorial on the matter and then ask any specific questions, preferably with some code example. – bcperth Oct 28 '18 at 02:49

1 Answers1

3

first of all it's important to understand that you will only call super(props) in a component if you need to use the props inside a constructor for a particular reason, by making use of super(props) you're enabling access to your component's props inside of the constructor.

If you don't really need to use props there, you could just use super(), however, it's recommended to use super(props) to avoid inconsistencies and missleading behaviors.

The constructor is executed before components are mounted and sometimes you want to use them to set up some things like React's local state, or bind some handlers.

There are other references to this topic in SO that could help you clarify your doubts:

Enmanuel Duran
  • 4,988
  • 3
  • 17
  • 29