将需要在setState
后执行的代码放在回调函数中
this.setState(()=>{ return { 设置state状态 };},()=>{修改状态后执行的代码})
样例如下:
this.setState(
() => {
return { expId: index };
},
() => {
const { expId, user } = this.state;
console.log(expId);
reqExpAccess(user.userId, expId)
.then(response => {
console.log("response.data", response.data);
window.location.href = "/#/experience";
})
.catch(error => {
console.log("failed", error);
message.error(error.message);
});
})