I a have post list and I am trying to call an action inside of constructor or componentDidMount for each post. But somehow when I send a new message constructor and componentDidMount functions are called twice.
constructor(props) {
super(props);
if (condition1) {
this.props.actions.action1();
} else if (condition2) {
this.props.actions.action2();
}
}
These functions are called only once when the posts are readed from a list. But when I send a new message they are called twice.
How can i avoid these situation. I tried to use componendDidUpdate function like this:
componentDidUpdate(prevProps, prevState) {
if (prevProps.post.id !== this.props.post.id) {
if (condition1) {
this.props.actions.action1();
} else if (condition2) {
this.props.actions.action2();
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…