reactjs - 在 componentDidmount 之前调用 ComponentWillUnmount
问题描述
我有一个名为 PAGE2 的组件,我将从该组件返回到组件 PAGE1。现在,当我使用不同的道具从 PAGE1 移动到 PAGE2 时,我看到 PAGE2 具有旧值(此处未调用包含 fetchListValues 函数的 componentDidMount)
我正在使用 navigation.navigate('PAGE1'); 从第 2 页到第 1 页。
componentDidMount = () => {
// in page 2
this.fetchChats();
this.backHandler = BackHandler.addEventListener(
"hardwareBackPress",
this.handleBackPress
);
};
handleBackPress() {
console.log('Back pressing', this.props.navigation);
this.props.navigation.navigate('PAGE1');
return true;
}
componentWillUnmount() {
console.log('Hitting component will unmount');
BackHandler.removeEventListener("hardwareBackPress",
this.handleBackPress);
}
解决方案
this.props.navigation.navigate('PAGE1');
//instead of this use the one below
this.props.navigation.goBack();
导航未卸载 PAGE2,因此未调用相关函数。
推荐阅读
- java - 将 Java 流计数为整数,而不是长
- ios - 如何在不出现键值编码兼容错误的情况下创建对 ScrollView 的引用出口
- linq - 如果where子句不匹配,想用linq返回所有数据
- azure - 在 Azure 数据工厂中添加日期列
- r - 在 R 中:按日期从数据框中提取特定列并将基本列保留在开头
- kubernetes - 使用 svc.cluster.local URL 访问 RabbitMQ
- mysql - 带有数据库连接的 NodeJS 集群
- c# - IQ 零售 API 不返回发票数据
- sql - 按工厂轮班时间对表数据(id、machine_id、telemetry_time、riskscore、current)进行分组,并将日期视为轮班开始时间
- git - 撤消 git checkout .. 稍作改动