2016-08-30 29 views
2

我有一個組件顯示了我從服務器檢索到的對象列表。我的實現如下所示。我已經嘗試了幾種生命週期方法,但他們似乎都沒有做我所需要的。什麼生命週期方法用於初始化React Redux組件中的數據?

我需要xxxxLifeCycleMethod(...)中的代碼在理想情況下被調用,但如果在這些屬性更改時調用它們(它們真的不會),它會很好。我最適合這種猜測將根據this linkcomponentWillReceiveProps(...)

class Home extends Component { 

    xxxxxLifeCycleMethod(){ 
    let { userId,token } = this.props; 
    this.props.dispatch(loadMyList(userId,token)); 
} 

... 

//Component Implementation 

... 

function mapStateToProps(state) { 
    return { 
    userId:state.get('profile').current.id, 
    token:state.get('application').auth.token, 
    } 
} 
+1

你是否嘗試過'componentWillReceiveProps'? 'componentDidMount'怎麼樣? – Blorgbeard

+1

我認爲你需要'componentWillMount'作爲'componentWillReceiveProps'每次改變prop而被調用,而componentWillMount'在組件最初被渲染前被調用一次。 [源代碼](https://facebook.github.io/react/docs/component-specs.html) – NoobsArePeople2

+0

componentDidMount將與您的用例相匹配,get組件的調用時只調用一次。 componentWillReceiveProps不會在組件第一次裝入時調用,但是對於未來的prop更新 – dnivra

回答

1

componentWillMount is executed before rendering, on both server and client side. 

componentDidMount is executed after first render only on the client side. This is where AJAX requests and DOM or state updates should occur. This method is also used for integration with other JavaScript frameworks and any functions with delayed execution like setTimeout or setInterval. We are using it to update the state so we can trigger the other lifecycle methods. 

我認爲你應該使用componentWillMount生命週期。

相關問題