**(Learning react notes) React class constructor write super(props)**

In the class constructor function, super refers to the constructor of the parent class. If you use this.props before calling super(props), you will directly report an error, and you can use this.props after calling.
Insert picture description here
The benefits of calling super(props) before using this:
Let’s imagine that we call a function before calling super(props). At first, this function is not used inside this function.
Insert picture description here
After a period of time, due to changes in requirements, we need to modify thisTestFunction. Function
Insert picture description here
But don't forget that before calling this function, we didn't call super and didn't define this.props, this function will go wrong directly.

Do I have to pass parameters when calling super()?
After trying to call super without parameters, using this.props directly in the constructor will output undefined, and using this.props in the function called by the constructor will also output undefined, and this.props can still be used outside the constructor.
Therefore, parameters are not mandatory in the constructor. If the parameters are not defined, the internal use of this.props in the constructor will be undefined. Of course, react will allocate this.props after the constructor runs.
Super() can be called without parameters, but it is not necessary
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_39139322/article/details/101059634