代码

import React, {Component} from 'react';

class Async extends Component {

    state = {count: 0}

    add = () => {
        // {count: this.state.count + 1}
        this.setState(() => ({count: this.state.count + 1}), () => {
            // 回调函数 获取更新的 state 值
            console.log('更新之后的值:', this.state.count)
        });
        console.log("更新前打印的值:", this.state.count)
    }

    render() {
        return (
            <div>
                <h2>{this.state.count}</h2>
                <button onClick={this.add}>增加</button>
            </div>
        );
    }
}

export default Async;

如何拿到setstate更新的值

this.setState({ counte: this.state.counte + 1 }, () => {
  console.log("更新后的值:", this.state.counte);
});

函数写法

this.setState(
  () => ({ counte: this.state.counte + 1 }),
  () => {console.log("更新后的值:", this.state.counte)}
);
12-01 13:56