我使用什么生命周期方法来初始化React Redux组件中的数据?

时间:2016-08-30 02:53:26

标签: reactjs react-native redux

我有一个组件,显示我从服务器检索的对象列表。我的实现如下所示。我尝试了几种生命周期方法,但它们似乎都不能满足我的需要。

我需要理想地调用xxxxLifeCycleMethod(...)中的代码,但是如果在这些属性发生变化时调用它们(他们真的不会),那就没问题了。我最好的猜测是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,
  }
 }

1 个答案:

答案 0 :(得分:1)

根据this link

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生命周期。