简体   繁体   English

什么是超级(道具)为我的React组件做什么?

[英]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 call super(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.

相关问题 `Component是什么意思 <Props> 反应本机 - What is the meaning of `Component<Props>` of react-native 将prop传递到React中的组件的正确方法是什么? - What is the correct way to pass props to component in React? 在 React 中将 props 传递给组件的更好方法是什么? - What is the better way for passing props to component in React? 在不在其中访问props的情况下,在React构造函数中将props传递给super()的原因是什么? - What is the reason for passing props to super() in a React constructor when props aren't accessed within it? React中的构造函数(props)和super(props)VS构造函数()和super() - Constructor(props) and super(props) VS constructor() and super() in React 当props进入组件时,为什么我的react组件没有重新渲染? - Why is my react component not re rendering when props comes into the component? 为什么 props 不能与我的子组件 React JS (react native) 一起使用 - Why props is not working with my child component React JS (react native) React Redux:我的 React 组件没有收到更新的数组作为道具 - React Redux : My React component not receive updated array as props 为什么 React 类组件总是需要在其构造函数中调用 super(props) ? - Why does react class component always need to call super(props) in its constructor? 将 props 传递给 React 组件 - Passing props into React component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM