【发布时间】:2019-05-23 02:50:12
【问题描述】:
我正在运行一个函数,该函数有一个 API 调用,它返回一个我用来设置状态的响应,然后触发另一个使用刚刚更新的状态的函数。第二个函数在响应返回并更新状态之前运行的问题
我试过了。然后我认为应该可以工作
import React from "react";
import API from "../../utils/API"
class Content extends React.Component {
state={
postID:"",
statusPost:""
}
submitPost = () => {
API.savePost({
content: this.state.statusPost,
post_by: this.props.userInfo.firstname + this.props.userInfo.lastname
})
.then(console.log(this.submitPost))
.then(res => {
this.setState({ postID:res.data._id })
console.log(this.state)
})
.then(this.addPostID())
.catch(err => console.log(err));
}
addPostID = () => {
API.postID({
_id: this.props.userInfo.user_ID,
post:this.state.postID
})
.then(res => console.log(res))
.catch(err => console.log(err));
}
}
【问题讨论】: