React native 基于上一条管线有条件地重新加载组件

React native 基于上一条管线有条件地重新加载组件,react-native,react-navigation,React Native,React Navigation,也许有人能帮我解决我在React导航(2.0.4)中遇到的React原生问题。我有一个页面(第C页-RouteShowScreen),我想有条件地重新加载,这取决于它是从第a页(RoutePrepareScreen)还是从第X页(导航到第C页的任何其他页面)导航到的,而我似乎无法获得正确的生命周期方法。为了清楚起见,如果从页面A(RoutePrepareScreen)导航到该页面,我希望该页面重新加载,但如果从任何其他屏幕导航到该页面,则不希望重新加载。我尝试使用willFocus监听器,但是不

也许有人能帮我解决我在
React导航(2.0.4)中遇到的React原生问题。我有一个页面(第C页-RouteShowScreen),我想有条件地重新加载,这取决于它是从第a页(RoutePrepareScreen)还是从第X页(导航到第C页的任何其他页面)导航到的,而我似乎无法获得正确的生命周期方法。为了清楚起见,如果从页面A(RoutePrepareScreen)导航到该页面,我希望该页面重新加载,但如果从任何其他屏幕导航到该页面,则不希望重新加载。我尝试使用
willFocus
监听器,但是不管是从A页还是B页导航到C页,C页都会重新加载

路由重新筛选

...

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();
  }
}
路由窗口屏幕

...

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);
然而,这对我来说有点脏(如果用例不能处理导航堆栈的完全重置,那么这可能是站不住脚的)。我很想看到另一个解决方案