2014-10-20 58 views
1

我有一个ReactJS组件,需要来自AJAX请求的响应才能呈现。我试图给商店添加一个监听器,以便当它发出loaded时,我在该组件上调用this.setProps(/*props*/)。这不起作用,因为组件的子项也依赖于正在加载的数据。在AJAX之后加载React组件

我也尝试过使用componentWillUpdatecomponentWillReceiveProps。我认为componentWillReceiveProps就是我正在寻找的东西,它确实适用于自己的主要观点,但它不会将数据传播给孩子。

任何想法?

回答

-3

问题解决

var Account = React.createClass({ 

    /** 
    * @cfg {String} 
    */ 
    displayName: 'Account', 

    /** 
    * Get initial state of component 
    */ 
    getInitialState: function() { 
    return { user: null, accounts: null }; 
    }, 

    /** 
    * Set state of component when UserStore is loaded 
    */ 
    onUserStoreLoaded: function() { 
    this.setState({ 
     user: UserStore.getUser(), 
     accounts: UserStore.getUserAccounts() 
    }); 
    }, 

    /** 
    * Perform logic when component is about to mount to 
    * the DOM 
    */ 
    componentWillMount: function() { 
    if (UserStore._user) { 
     this.onUserStoreLoaded(); 
    } else { 
     UserStore.on('loaded', this.onUserStoreLoaded); 
    } 
    }, 

    render: function() { 
    return (
     <div className="col-sm-6 col-sm-offset-3"> 
     <h1 className="text-center"> Account </h1> 
     <ConnectedAccounts accounts={this.state.accounts} /> 
     </div> 
    ); 
    } 

}); 
+1

是什么'UserStore'? – Maslow 2014-12-02 15:48:11

+0

是的,请分享'UserStore'代码。 – 2015-03-24 11:48:11