Resetting the navigation stack for the home screen (React Navigation and React Native)

前端 未结 13 2406
终归单人心
终归单人心 2020-11-29 23:49

I\'ve got a problem with the navigation of React Navigation and React Native. It is about resetting navigation and returning to the home screen.

I\'ve build a StackN

13条回答
  •  Happy的楠姐
    2020-11-30 00:18

    To use Back, you need to find the unique key associated with the path. Inside your navigation reducer, you can search the existing state to find the first route on the stack using that path, grab its key & pass that to Back. Back will then navigate to the screen prior to the path you gave.

      let key;
    
      if (action.payload) {
        // find first key associated with the route
        const route = action.payload;
    
        const routeObj = state.routes.find( (r) => r.routeName === route );
    
        if (routeObj) {
          key = { key: routeObj.key };
        }
      }
    
      return AppNavigator.router.getStateForAction( NavigationActions.back( key ), state );
    

提交回复
热议问题