react constructor

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
相关文章
相关标签/搜索