《my React demo 1》
增加一个点击计数。
1. initialize
constructor() { /// this code }
1) bind this
super();
2)vector
this.state
this.state = {iCount: 0};
3) bind value
Binds the value to the specified method.
this.myClick = this.myClick.bind(this);
2. ACC
alert(this.state.iCount); this.state.iCount++;
update code:
import React, { Component } from 'react'; class Count extends Component { constructor() { super(); this.state = {iCount: 0}; this.myClick = this.myClick.bind(this); } myClick() { alert(this.state.iCount); this.state.iCount++; } render() { return( <div> <button onClick={this.myClick}>Click me</button> </div> ); } } export default Count;