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