也许有人可以帮助我解决我在react-navigation
(2.0.4)中遇到的React Native问题。我有一个页面(页面 C - RouteShowScreen),我想有条件地重新加载,具体取决于它是从页面 A (RoutePrepareScreen) 还是页面 X(导航到页面 C 的任何其他页面)导航到的,我似乎无法正确获取生命周期方法。需要明确的是,如果从页面 A (RoutePrepareScreen) 导航到页面,我希望页面重新加载,但如果从任何其他屏幕导航到页面,则不会。 我尝试使用 willFocus
侦听器,但随后页面 C 重新加载,无论它是从页面 A 还是页面 B 导航到的。
路由准备屏幕
...
this.props.navigation.navigate("RouteShow", {
currentLat: latitude,
currentLng: longitude,
destinationAddress: this.state.destinationAddress,
currentAddress: this.formatCurrentAddress(),
destinationLat,
destinationLng,
speed,
hoursUntilBreak,
estimatedShiftLength});
}
...
路线显示屏幕
/** This caused the page to reload, regardless of how it was navigated to **/
willFocus = this.props.navigation.addListener(
'willFocus',
() => {
this.handleRouteLoad();
}
);
/** I also tried using componentWillReceiveProps and adding an additional "reload" navigation parameter, but this threw the
app into an infinite loop **/
componentWillReceiveProps(nextProps) {
if (nextProps.navigation.state.params.reload) {
this.handleRouteLoad();
}
}
在我的特殊情况下,我能够通过在从RoutePrepareScreen
内导航时重置导航堆栈来使其工作 - 如下所示:
const resetAction = StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({
routeName: "RouteShow",
params: {
currentLat: latitude,
currentLng: longitude,
destinationAddress: this.state.destinationAddress,
currentAddress: this.formatCurrentAddress(),
destinationLat,
destinationLng,
speed,
hoursUntilBreak,
estimatedShiftLength,
}
})
],
});
this.props.navigation.dispatch(resetAction);
但是,这对我来说感觉有点脏(如果用例无法处理导航堆栈的完全重置,则可能站不住脚)。我很想看到另一种解决方案!