React,为什么在ES6类构造函数里面使用super(props)? [英] React, why use super(props) inside of ES6 class constructor?

查看:646
本文介绍了React,为什么在ES6类构造函数里面使用super(props)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I realize the super keyword can be used to call functions in a parent component. However, I'm not totally clear why you would use the super keyword in the example below - just passing it whatever props are being passed to the constructor.

有人可以在ES6类构造函数中使用超级关键字的各种原因作出反应吗?

Can someone please shed some light on the various reasons for using the super keyword in an ES6 class constructor, in react?

  constructor(props) {
    super(props);

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

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


推荐答案

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

super allows you to access the constructor method of the parent class. The only reason to include props is to access this.props inside of your constructor.

super()之间的区别是什么?和超(道具)在使用es6类时的React?

这篇关于React,为什么在ES6类构造函数里面使用super(props)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆