ReactJS async, wait for results

前端 未结 2 1065
青春惊慌失措
青春惊慌失措 2020-12-30 00:17

I am new to ReactJS and trying to understand it. Now I have a situation where I am loading information needed for rendering. But as it is asynchronous the component renders

2条回答
  •  别那么骄傲
    2020-12-30 01:09

    You need to do something like below:

    var InfoPage = React.createClass({
      getInitialState: function() {
         return {info: "loading ... "};
      },
      componentDidMount: function() {
         this.getInfo();
      },
      render: function() {        
        return (
            
    info: {this.state.info}
    ); }, getInfo:function(){ $.ajax({ url:"restapi/getInfo/whatever", .... }).success(function(res){ this.setState({info:res}); }.bind(this)); } });

提交回复
热议问题