javascript - 在没有事件的情况下如何使用参数“COUNTRY_ID”发布到 API
问题描述
这是我的状态
constructor(props){
super(props)
this.state = {
merchants:[],
}
}
这是反应生命周期
componentWillMount() {
return this.setState({
COUNTRY_ID: this.props.resGet('country_id')
})
}
async componentDidMount(){
await Api.post('language/merchant', this.state)
.then((response) => {
let responseJson = response
console.log(responseJson)
if(responseJson.data.STATUS_CODE === '200'){
this.setState({
merchants:response.data.DATA
})
}
})
}
这是带有"merchant []"
属性的初始状态,然后"merchant []"
将填充请求的结果,该结果将通过参数发布到API "COUNTRY_ID"
,如何进入"COUNTRY_ID"
状态然后我将参数发布" COUNTRY_ID "
到API?
解决方案
被认为是遗留问题,您应该在新代码中避免使用它们。
componentDidMount
将在之后render
立即执行componentWillMount
,因此,如果componentWillMount
具有异步函数,则在调用时可能找不到它们的结果componentDidMount
,
你可以把你有的东西componentWillMount
放进去componentDidMount
:
async componentDidMount(){
const countryID = this.props.resGet('country_id');
await Api.post('language/merchant', countryID)
.then((response) => {
let responseJson = response
console.log(responseJson)
if(responseJson.data.STATUS_CODE === '200'){
this.setState({
merchants:response.data.DATA,
COUNTRY_ID: countryID
})
}
})
}
推荐阅读
- rust - Rust trait 由 supertrait 绑定,自身作为泛型参数
- java - 错误:android.view.WindowManager$BadTokenException:无法添加窗口——令牌 null 无效;您的活动正在运行吗?
- django - 错误子目录中的静态文件夹
- html - 导航栏未显示所有标题
- arrays - 如何在结构中附加切片
- assembly - 如何让代码在 Apple ii 上正确运行
- html - 如何在此标题栏中将标题居中?
- asp.net - 将 Docker 映像保存 IP 地址从 Windows 传输到 Linux
- c - 在C编程++arrayname[value[i]/10]中执行这条语句时,对数组做了什么动作?
- windows-10 - 如何将 explorer.exe 设置为在 Windows 10 启动时运行?