2014-10-20 60 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