1传入一个对象
import React, { Component } from 'react'
export default class App extends Component {
clickHandle=()=>{
const {count}=this.state
this.setState({
count:count+1
},()=>{
//这里可以拿到最新的count值
console.log(this.state.count)
})
}
state={
count:0
}
render() {
const {count}=this.state
return (
<div>
<h1>这是一个数据:{count}</h1>
<button onClick={this.clickHandle}>加一</button>
</div>
)
}
}
2.传入一个函数
import React, { Component } from 'react'
export default class App extends Component {
clickHandle=()=>{
this.setState((state,props)=>{
return{
count:state.count+1
}
},()=>{
console.log(this.state.count)
})
}
state={
count:0
}
render() {
const {count}=this.state
return (
<div>
<h1>这是一个数据:{count}</h1>
<button onClick={this.clickHandle}>加一</button>
</div>
)
}
}