我有一个帖子列表,我试图在每个帖子的构造函数或 componentDidMount 中调用一个动作。但不知何故,当我发送一个新的消息构造函数和 componentDidMount 函数被调用两次。
constructor(props) {
super(props);
if (condition1) {
this.props.actions.action1();
} else if (condition2) {
this.props.actions.action2();
}
}
这些函数仅在从列表中读取帖子时调用一次。但是当我发送一条新消息时,它们会被调用两次。
我怎样才能避免这些情况。我尝试像这样使用 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();
}
}
}