【问题标题】:React, why use super(props) inside of ES6 class constructor? [duplicate]React,为什么在 ES6 类构造函数中使用 super(props)? [复制]
【发布时间】:2016-10-19 20:40:45
【问题描述】:

我意识到 super 关键字可以用来调用父组件中的函数。但是,我不完全清楚为什么要在下面的示例中使用 super 关键字 - 只需将传递给构造函数的任何道具传递给它。

有人能解释一下在 ES6 类构造函数中使用 super 关键字的各种原因吗?

  constructor(props) {
    super(props);

    this.state = {
      course: Object.assign({}, this.props.course),
      errors: {   }
    };

    this.updateCourseState = this.updateCourseState.bind(this);
  }

【问题讨论】:

  • 查看类声明会很有帮助。例如class SpecialDialog extends Dialog {

标签: reactjs ecmascript-6 super


【解决方案1】:

super 允许你访问父类的构造方法。包含 props 的唯一原因是在构造函数中访问 this.props。

What's the difference between "super()" and "super(props)" in React when using es6 classes?

【讨论】:

  • 非常感谢。这回答了我的问题。
  • 有人可以扩展一下吗?那么React.Component有自己的构造方法,我们使用super()来触发呢?
猜你喜欢
  • 2018-05-06
  • 1970-01-01
  • 2017-05-23
  • 1970-01-01
  • 2016-11-26
  • 2017-07-23
  • 2013-04-25
相关资源
最近更新 更多