javascript - How to do render only after http request completed in ReactJS -
i need render function of component called after request componentdidmount function completed.
componentdidmount(){ let ctx = this; apiservice.get('/busca/empresa/pagina').then(function(response){ if(response.data.empresa){ ctx.setstate({company:response.data.empresa}); ctx.getproducts(); ctx.verifyauthentication(); } }, function(error){ notification.error('http status: ' + error.response.status + ' - ' + error.response.data.mensagem); }); }
the problem when open page render function invoked before componentdidmount completed. returning function else condition (rendernotvalidatecompany) , returning rendervalidatecompany after updating this.state.company.
render(){ if(this.state.company){ return this.rendervalidatecompany(); }else{ return this.rendernotvalidatecompany(); } }
is possible rendering called when componentdidmount completed in react?
thanks!
like said in comment, store request status in state , render based on it:
this.state = { company:null, requestcompleted:false, }
and in render method:
render(){ if(this.state.requestcompleted && this.state.company){ return this.rendervalidatecompany(); } else if (this.state.requestcompleted){ return this.rendernotvalidatecompany(); } else { return <loadinggif /> {/*or return null*/} } }
ofcourse update request status in promise.
wiki
Comments
Post a Comment