1传入一个对象

  1. import React, { Component } from 'react'
  2. export default class App extends Component {
  3. clickHandle=()=>{
  4. const {count}=this.state
  5. this.setState({
  6. count:count+1
  7. },()=>{
  8. //这里可以拿到最新的count值
  9. console.log(this.state.count)
  10. })
  11. }
  12. state={
  13. count:0
  14. }
  15. render() {
  16. const {count}=this.state
  17. return (
  18. <div>
  19. <h1>这是一个数据:{count}</h1>
  20. <button onClick={this.clickHandle}>加一</button>
  21. </div>
  22. )
  23. }
  24. }

2.传入一个函数

  1. import React, { Component } from 'react'
  2. export default class App extends Component {
  3. clickHandle=()=>{
  4. this.setState((state,props)=>{
  5. return{
  6. count:state.count+1
  7. }
  8. },()=>{
  9. console.log(this.state.count)
  10. })
  11. }
  12. state={
  13. count:0
  14. }
  15. render() {
  16. const {count}=this.state
  17. return (
  18. <div>
  19. <h1>这是一个数据:{count}</h1>
  20. <button onClick={this.clickHandle}>加一</button>
  21. </div>
  22. )
  23. }
  24. }