Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some developers use constructor and super in their class in React.js?

I always use this expression in my components:

class Cart extends Component { }

Recently I have seen a lot of codes using this expression.

class Cart extends React.Component {
  constructor(props) {
    super(props);
  }
}

Why is it? What is the purpose of using constructor and super? Is React.Component and Component same? Why do we pass props in constructor and super? I am not asking what super and constructor are, I am asking difference between 2 codes above and benefits of using each one?

I checked online but did not see any explanation, just code examples.

like image 584
Yilmaz Avatar asked Jun 07 '19 22:06

Yilmaz


People also ask

Why we use constructor and super in React JS?

In React, constructors are mainly used for two purposes: It used for initializing the local state of the component by assigning an object to this. state. It used for binding event handler methods that occur in your component.

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 use super in React class component?

When we call this super(props), we are basically calling the constructor of React. Component. So we can say that super() is a reference to the parent class constructor i.e. React. Component in the above example, which is also the base class for Person component.

Why would you use super constructors with props arguments?

Using super constructor with props arguments basically allows accessing this. props in a Constructor function. Let us create a React project and then we will create a UI to showcase the above purpose. Users will be able to see the application of the super constructor with props argument.


1 Answers

Constructors and super are not react specific or even javascript specific. They are specific to the inheritance in OOP.

Constructors

Constructors are what can be called as initializing functions in a class. Let's look at an example where a constructor can be used.

class parentClass {
 constructor(){
   this.foo = foo;
   this.bar = bar;
}

function sharedMethod1(){
    print(this.foo);
}

function sharedMethod(){
    print(this.bar)
}
}

object1 = new ParentClass(foo1, bar1);

object1.sharedMethod1() // this will print foo1;
object1.sharedMethod2() // this will print bar1;

object2 = new ParentClass(foo2, bar2);
object2.sharedMethod1() // this will print foo2;
object2.sharedMethod2() // this will print bar2;

when there is a need to create multiple instances of a class with different values for member variables / functions, we make use of the constructor functions.

Super

The super keyword is used in inheritance as well. In inheritance when extending a child class from a parent class, there is a need to initialise the constructor of the parent class. The super keyword is used for this purpose. let's look at the below example for super.

class ParentClass (){
constructor(){
this.foo = foo;
}
} 

class childClass extends ParentClass(){
super(foo1); // super is used here initialize the constructor of the ParentClass
} 

The same principle mentioned above is followed in React as well. Please look into dan abramov's blog post on constructor and super here https://overreacted.io/why-do-we-write-super-props/

like image 157
harinarayanan Avatar answered Oct 18 '22 05:10

harinarayanan