React中setState回调

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hxy19971101/article/details/81158717

setState()是异步的

1. 语法:

setState中必须在回调中打印,直接输出将不会变化!

setState(updater[, callback])  

this.state={
  test: false
}
this.setState({test: true});
console.log(this.state.test); // false
//使用回调
this.state = {test: false};
this.setState(
    {
        test: true
    }, ()=> {
        console.log(test); // true
    }
);

猜你喜欢

转载自blog.csdn.net/hxy19971101/article/details/81158717
今日推荐