2
我有一個組件顯示了我從服務器檢索到的對象列表。我的實現如下所示。我已經嘗試了幾種生命週期方法,但他們似乎都沒有做我所需要的。什麼生命週期方法用於初始化React Redux組件中的數據?
我需要xxxxLifeCycleMethod(...)
中的代碼在理想情況下被調用,但如果在這些屬性更改時調用它們(它們真的不會),它會很好。我最適合這種猜測將根據this link是componentWillReceiveProps(...)
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,
}
}
你是否嘗試過'componentWillReceiveProps'? 'componentDidMount'怎麼樣? – Blorgbeard
我認爲你需要'componentWillMount'作爲'componentWillReceiveProps'每次改變prop而被調用,而componentWillMount'在組件最初被渲染前被調用一次。 [源代碼](https://facebook.github.io/react/docs/component-specs.html) – NoobsArePeople2
componentDidMount將與您的用例相匹配,get組件的調用時只調用一次。 componentWillReceiveProps不會在組件第一次裝入時調用,但是對於未來的prop更新 – dnivra