How to wait for AJAX response and only after that render the component?

后端 未结 4 1878
粉色の甜心
粉色の甜心 2020-12-04 12:27

I have a problem with one of my React components. I think AJAX doesn\'t load all the content from external server before React renders the ChildComp component.<

4条回答
  •  余生分开走
    2020-12-04 12:55

    Just do the AJAX call in componentDidMount() and in render you can simply check...

    if(this.state.response === '' || this.state.response === [] || this.state.response === undefined)
    return 
    else{
    
    // do your stuff here
    
    }
    

    OR if you are using react 16.6 or more, you can simply use lazy components.

提交回复
热议问题