【发布时间】:2017-05-28 20:21:04
【问题描述】:
是否可以在.then 回调中返回函数?
我想做这样的事情:
axios.post('url_api/endpoint')
.then(function(response){
renderAdmissionReponse(){
<div>Success response.data</div>
}
})
稍后在这样的组件中渲染它:“
<div className="row spacer">
{this.renderAdmissionReponse()}
</div>
有可能吗?
【问题讨论】:
-
否。由于它的反应,您可以在外部渲染中调用该函数,
then将更新最终更新 UI 的state -
好吧,基本上是这样的:
axios.post('url_api/endpoint') .then(function(response){ this.setState({ admissionResponse: response.data }) }) -
是的,
admissionResponse格式是什么? -
它是 JSON 格式
-
它是一个对象内容:为了渲染我这样做了:
const admissionResponseJSON = JSON.stringify(this.state.admissionResponse, null, 2)