react constructor

527 阅读1分钟
constructor(props){

    super(props);

    this.state = {

    };

}
  • constructor必须用super()初始化this, 可以绑定事件到this
  • 如果你在constructor中要使用this.props, 就必须给super加参数, super(props);
  • 无论有没有constructor, render中都可以使用this.props, 默认自带
  • 如果组件没有声明constructor, react会默认添加一个空的constructor
  • ES6采用的是先创建父类的实例this(故要先调用 super( )方法),完后再用子类的构造函数修改this