React,为什么在 ES6 类构造函数中使用 super(props) ?

IT技术 reactjs ecmascript-6 super
2021-04-14 00:31:01

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

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

  constructor(props) {
    super(props);

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

    this.updateCourseState = this.updateCourseState.bind(this);
  }
1个回答

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

使用 es6 类时,React 中的“super()”和“super(props)”有什么区别?

有人可以扩展一下吗?那么React.Component有它自己的构造函数方法,我们super()用来触发它吗?
2021-05-25 00:31:01
万分谢意。这回答了我的问题。
2021-06-11 00:31:01