判斷一個元素是HTML還是React組件的原則就是看第一個字母是否大寫,
import React,{Component} from 'react';
class ClickCounter extends Component{
constructor(props){
super(props);
this.onClickButton = this.onClickButton.bind(this);
this.state = {count:0};
}
onClickButton(){
this.setState({count:this.state.count+1});
}
render(){
const counterStyle ={
margin:'26px'
}
return(
<div style={counterStyle}>
<button onClick ={this.onClickButton}> Click Me</button>
<div>
Click Count:<span id="clickCount">{this.state.count}</span>
</div>
</div>
);
}
}
export default ClickCounter;