[英]What is super(props) doing for my React component?
I am not great with JS and playing around with React. 我对JS并不熟悉并且使用React。
The React docs located here state the following: 位于此处的React文档声明如下:
When implementing the constructor for a
React.Component
subclass, you should callsuper(props)
before any other statement.在为
React.Component
子类实现构造函数时,应该在任何其他语句之前调用super(props)
。 Otherwise,this.props
will be undefined in the constructor, which can lead to bugs.否则,
this.props
将在构造函数中未定义,这可能导致错误。
My question is HOW does this actually work? 我的问题是这实际上是如何工作的? What is
super()
doing that magically enables this.props
within my constructor? 什么是
super()
在我的构造函数中神奇地启用this.props
?
In the documentation that you have mentioned. 在您提到的文档中。 It is coded in ES6 standard of javascript.
它是用javascript的ES6标准编码的。
So this statement 所以这句话
class Greeting extends React.Component
It means Greeting is inherting from React.Component
, by calling super
, you are actually calling the parent element with props
parameter, 这意味着问候语从
React.Component
,通过调用super
,你实际上是用props
参数调用父元素,
if you intend on using this.props
inside the constructor
, you have to call super(props)
如果你打算在
constructor
使用this.props
,你必须调用super(props)
Hope these links are helpful. 希望这些链接很有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.