setState doesn't update the state immediately

前端 未结 12 2485
暖寄归人
暖寄归人 2020-11-21 05:25

I would like to ask why my state is not changing when I do an onclick event. I\'ve search a while ago that I need to bind the onclick function in constructor but still the s

12条回答
  •  执念已碎
    2020-11-21 05:43

    Since setSatate is a asynchronous function so you need to console the state as a callback like this.

    openAddBoardModal(){
        this.setState({ boardAddModalShow: true }, () => {
            console.log(this.state.boardAddModalShow)
        });
    }
    

提交回复
热议问题