current position:Home>Vue to react ----- can the constructor be omitted when using ES6?

Vue to react ----- can the constructor be omitted when using ES6?

2022-04-29 04:25:43baoleilei6

class XXX extends Component {
constructor(props) {
super(props);
this.state = {
};
}

}
This way of writing is similar to

class XXX extends Component {
state = {
}
}

Is there any difference in writing ? this Keywords are used normally

author :Hull Qin
link :https://www.zhihu.com/question/282019729/answer/1100603256
source : You know
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
 

If the subclass does not write a constructor, it will use the constructor of the parent class , Subclasses override the constructor of the parent class only when they want to have their own constructor , The subclass wants to have its own constructor and call the constructor of the parent class , Just use super. You can see the following example diagram 1 chart 2.

The two ways you say are really equivalent , Look at the picture 3.

chart 1、 No write constructor

chart 2、 Write constructor , But only the parent constructor is called

copyright notice
author[baoleilei6],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/117/202204270552176693.html

Random recommended