constructor(props) { super(props); this.state = { counter: 0 }; //初始化state this.handleClick = this.handleClick.bind(this); // 事件绑定 }
能够经过属性的方法初始化,Babel将会在后台自动加上constructorreact
class Foo extends Component { state = { loading: true }; }
使用es6箭头函数,将不须要将事件在constructor中绑定。es6
在react中能够不使用constructor函数