小编bur*_*nlu的帖子

react.js 构造函数调用了两次

我有一个帖子列表,我试图在每个帖子的构造函数或 componentDidMount 中调用一个动作。但不知何故,当我发送一个新的消息构造函数和 componentDidMount 函数被调用两次。

 constructor(props) {
    super(props);

    if (condition1) {
       this.props.actions.action1();
    } else if (condition2) {
       this.props.actions.action2();
    }    
}
Run Code Online (Sandbox Code Playgroud)

当从列表中读取帖子时,这些函数仅被调用一次。但是当我发送一条新消息时,它们会被调用两次。

我怎样才能避免这些情况。我尝试像这样使用 componendDidUpdate 函数:

componentDidUpdate(prevProps, prevState) {     
      if (prevProps.post.id !== this.props.post.id) {
         if (condition1) {
            this.props.actions.action1();
         } else if (condition2) {
            this.props.actions.action2();
         }
      }   
}
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-redux

8
推荐指数
2
解决办法
6367
查看次数

标签 统计

javascript ×1

react-redux ×1

reactjs ×1