1 function 组件
let n = 0;
function App() {
return (
<div className="App">{n}
<button onClick={add}>加一</button>
</div>
);
}
function add (){
n+=1
render()
}
render()
function render(){
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement)
};
2 class组件
class App extends React.Component {
constructor(props){
super(props)
this.state = {
n : 0
}
}
add(){
this.setState({
n:this.state.n + 1
})
}
render(){
return(
<div>{this.state.n}
<button onClick={this.add.bind(this)}>加一</button>
</div>
)
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
网友评论