Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does super() do without any arguments?

I'm learning react from the docs, but not sure what the super() does in this example. Usually, doesn't it take the arguments that are passed to making a new instance and then calls React.Component's constructor method to incorporate these arguments into the instance? What does it do without any arguments?

class LikeButton extends React.Component {   constructor() {     super();     this.state = {       liked: false     };     this.handleClick = this.handleClick.bind(this);   }   handleClick() {     this.setState({liked: !this.state.liked});   }   render() {     const text = this.state.liked ? 'liked' : 'haven\'t liked';     return (       <div onClick={this.handleClick}>         You {text} this. Click to toggle.       </div>     );   } }  ReactDOM.render(   <LikeButton />,   document.getElementById('example') ); 
like image 398
mangocaptain Avatar asked Oct 02 '16 23:10

mangocaptain


People also ask

What is the super () method used for?

Definition and Usage The super() function is used to give access to methods and properties of a parent or sibling class. The super() function returns an object that represents the parent class.

What would happen if this () and super () used in a method?

this(...) will call another constructor in the same class whereas super() will call a super constructor. If there is no super() in a constructor the compiler will add one implicitly. Thus if both were allowed you could end up calling the super constructor twice.

Is super () necessary JavaScript?

The fish class doesn't need super() because its “parent” is just the JavaScript Object. Fish is already at the top of the prototypal inheritance chain, so calling super() is not necessary — fish's this context only needs to include Object, which JavaScript already knows about.

Why do we need super () in an extended class?

The super keyword is used to call the constructor of its parent class to access the parent's properties and methods. Tip: To understand the "inheritance" concept (parent and child classes) better, read our JavaScript Classes Tutorial.


1 Answers

In ES6, derived classes have to call super() if they have a constructor. In react, all components extend from the Component class.

You don't actually need a constructor for every ES6/react class. If no custom constructor is defined, it will use the default constructor. For base classes, it is:

constructor() {} 

And for derived classes, the default constructor is:

constructor(...args) {   super(...args); } 

You also need to call super() before accessing this, since this is not initialized until super() is called.

There are a few reasons to use a custom constructor in react. One is that you can set the initial state within the constructor using this.state = ... instead of using the getInitialState lifecycle method.

You can also bind class methods inside the constructor with this.someClassMethod = this.someClassMethod.bind(this). It's actually better to bind methods in the constructor since they will only be created once. Otherwise if you call bind or use arrow functions to bind methods anywhere outside the constructor (like in the render method), it will actually end up creating a new instance of the function on every render. Read more about that here.

If you want to use this.props in the constructor, you need to call super with props as an argument:

constructor(props) {   super(props);   this.state = {count: props.initialCount}; } 

If you don't, then this.props is undefined in the constructor. However, you can still access this.props anywhere else in the class outside the constructor without needing to do anything with it in the constructor.

like image 153
Varun Munjeti Avatar answered Sep 24 '22 02:09

Varun Munjeti