Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does react class component always need to call super(props) in its constructor?

In the tutorial published by reactjs.org, it was stated that "Class components should always call the base constructor with props". In my own research, it seems that super(props) can be replaced by super() if this.props is not used in the constructors, according to this StackOverflow answer.

Therefore, my question is, why should we always pass in props to base constructor in reactjs? Is the advice sound? Why is the advice sound (or not sound)?

P.S. A screenshot is uploaded to this question just in case the original tutorial is updated while this question is being answered.

reactjs tutorial

like image 366
Lil E Avatar asked Jan 30 '19 05:01

Lil E


People also ask

Why do we need to call Super inside constructor in class based components?

super() will call the constructor of its parent class. This is required when you need to access some variables from the parent class. Hope this helps!

Why do we call super in constructor in React?

The super() is used inside the constructor for the purpose to get access to this keyword inside our constructor. With this let's try to understand super() first. Super is a keyword in JavaScript and is used to call super or parent class in the hierarchy. React class extends React Component with ES6 syntax.

Why we use constructor props and super props in React?

The constructor for a React component is called before it is mounted. When implementing the constructor for a React.Component subclass, you should call super(props) before any other statement. Otherwise, this.props will be undefined in the constructor, which can lead to bugs.

Why do we call super props?

Defining constructor and calling super(props) was always intended to be a temporary solution until class fields provide an ergonomic alternative.

When to call Super(props) in the constructor of a React component?

When implementing the constructor for a React.Component subclass, you should call super (props) before any other statement. Otherwise, this.props will be undefined in the constructor, which can lead to bugs. class TodoApp extends React.Component { constructor(){ super(); console.log(this.props); // this will be undefined } ... }

What is the difference between Super() and props() in react?

By calling super (props), you are calling the constructor of React.Component. Which means that super () is a reference to the parent class constructor () which in the above case is of React.Component which is the base class of Component Clock. When you pass props to super, the props get assigned to this. Another thing is that:

Why do we call Super (props) in constructor?

Defining constructor and calling super (props) was always intended to be a temporary solution until class fields provide an ergonomic alternative. But let’s get back to this example using only ES2015 features: Why do we call super? Can we not call it? If we have to call it, what happens if we don’t pass props? Are there any other arguments?

What does Super call the constructor of its parent class?

147 super()will call the constructorof its parentclass. This is required when you need to access some variables from the parent class. In React, when you call superwith props, React will make propsavailable across the component through this.props.


2 Answers

Although it is suggested to pass props to super but it isn't strictly necessary.

Passing it just helps in an odd situation where you might have a called a method in constructor and then at some point of time in future decided to use props in it. Now since props aren't available in the constructor since you haven't passed props to super, it would result in an error. This kind of situation might be tricky to debug and hence its recommended to pass props always whenever you write a constructor so that it ensures this.props is set even before the constructor exits.

like image 57
Shubham Khatri Avatar answered Sep 21 '22 08:09

Shubham Khatri


In JavaScript, super refers to the parent class constructor.

Why do we call super? Can we not call it? If we have to call it, what happens if we don’t pass props? Are there any other arguments?

Read the article that is linked below to get the answers for the above questions.

Read the article from Dan Abramov (Works at React JS team)

like image 37
Rajendran Nadar Avatar answered Sep 19 '22 08:09

Rajendran Nadar