我在componentDidMount
中触发了一个动作:
componentDidMount() {
this.props.getQuests();
}
,并且要在完成后再执行另一项操作,因此我使用了生命周期方法componentWillReceiveProps
componentWillReceiveProps = async nextProps => {
if (nextProps.questObject.length !== 0) {
const authToken = await AsyncStorage.getItem("authToken");
this.props.getProfile(authToken);
}
};
但是仅触发this.props.getQuests();
,而没有触发第二个动作this.props.getProfile(authToken)
。
const mapStateToProps = ({ quest, profile }) => {
const { error, loading, questObject } = quest;
const { profileObj } = profile;
return { error, loading, questObject, profileObj };
};
export default connect(
mapStateToProps,
{ getQuests, getProfile }
)(HomeScreen);
当前它返回以下错误:
Warning: Can't call setState (or forceUpdate) on an unmounted
component. This is a no-op, but it indicates a memory leak in your
application. To fix, cancel all subscriptions and asynchronous tasks in
the componentWillUnmount method.
答案 0 :(得分:1)
您正在尝试在安装DOM之前对其进行更新。使用componentWillReceiveProps
时,请确保将this.props
和最新的道具nextProps
进行比较。否则,{ {1}}会不必要地渲染组件,从而可能导致无限循环。
componentWillReceiveProps