也许有人可以帮助我解决react-navigation
(2.0.4)遇到的React Native问题。我有一个页面(页面C-RouteShowScreen)要有条件地重新加载,具体取决于是否从页面A(RoutePrepareScreen)或页面X(任何其他页面导航至页面C)导航到该页面,而我却无法似乎正确的生命周期方法。需要明确的是,如果页面是从页面A(RoutePrepareScreen)导航到的,我希望页面重新加载,但是如果页面是从任何其他屏幕导航到的,则不希望重新加载。我尝试使用willFocus
侦听器,但是无论页面C是从页面A还是页面B导航到的,页面C都会重新加载。
RoutePrepareScreen
...
this.props.navigation.navigate("RouteShow", {
currentLat: latitude,
currentLng: longitude,
destinationAddress: this.state.destinationAddress,
currentAddress: this.formatCurrentAddress(),
destinationLat,
destinationLng,
speed,
hoursUntilBreak,
estimatedShiftLength});
}
...
RouteShowScreen
/** 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();
}
}
答案 0 :(得分:0)
在我的特定情况下,我可以通过从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);
但是,这对我来说有点脏(如果用例无法处理导航堆栈的完全重置,则可能无法维持)。我很想知道其他解决方案!