6

Im try to understand how to reset in nested stack this my code

    const AuthStack = createStackNavigator(
      {
        Welcome,
        Login,
        Register,
        ConfirmationCode,
      },
      {
        initialRouteName: 'Welcome',
        headerMode: 'none',
        lazy: true,
        transitionConfig,
        defaultNavigationOptions: {
          gesturesEnabled: false,
        },
      }
    )

    const AppStack = createStackNavigator(
      {
        TabStack,
        SearchResult,
        BusinessDetail,
        BusinessMap,
        MakeAppointment,
        TermsAndConditions
      },
      {
        initialRouteName: 'TabStack',
        headerMode: 'none',
        lazy: true,
        transitionConfig,
        defaultNavigationOptions: {
          gesturesEnabled: false,
        },
      }
    )

    let MainStack = createSwitchNavigator(
      {
        AuthLoading,
        Auth: AuthStack,
        App: AppStack,
      },
      {
        initialRouteName: 'AuthLoading',
        headerMode: 'none',
        lazy: true,

        defaultNavigationOptions: {
          gesturesEnabled: false,
        },
      }
    )

TabStack

    import React from 'react';

    import { createBottomTabNavigator, createAppContainer } from 'react-navigation';
    import {
        Search,
        MyFavourites,
        MyAppointments,
        UserProfile
    } from '../screens'
    import Icon from 'react-native-vector-icons/Feather';
    import Colors from '../utils/Colors'
    let TabStack = createBottomTabNavigator(
      {
        Search,
         MyFavourites,
         MyAppointments,
         UserProfile,
      },
        initialRouteName: 'ScreenTab1',
        tabBarOptions: {
          activeTintColor: Colors.pink,
          inactiveTintColor: Colors.black,
          showLabel: false,
          style: {
            backgroundColor: 'white'
          }
        },
      }
    )
    export default createAppContainer(TabStack);

I want to understand how to make reset for example:

    reset from UserProfile to TabStack (in AppStack) to AuthStack

I tried to do from it this way

const resetAction = StackActions.reset({
        index: 0,
        actions: [NavigationActions.navigate({ routeName: 'AuthStack' })],
    });
    this.props.navigation.dispatch(resetAction);

or this way

const resetAction = StackActions.reset({
        index: 0,
        key: null,
        actions: [NavigationActions.navigate({ routeName: 'AuthStack' })],
    });
    this.props.navigation.dispatch(resetAction);

but i got the error

there is no route defined for AuthStack

I checked in issues in stackoverflow but the answers there not works for me,always show me the same error I wrote above.

4
  • 1
    Possible duplicate of React Native navigating between Nested StackNavigator Commented Feb 19, 2019 at 3:31
  • @JaydeepGalani I tried this solution, not working, it show the same error. Commented Feb 19, 2019 at 5:28
  • Check your route name, it should be Auth instead of AuthStack as your declaration :) And reset with key null will work cause it'll reset the root nav. Commented Feb 23, 2019 at 9:07
  • I did as you can see here, same error pastebin.com/hvJ7Mp7b Commented Feb 23, 2019 at 16:25

3 Answers 3

4
+200

Your resetAction is unsuccessful because you are dispatching it on TabStack (because you are calling this.props.navigation.dispatch on UserProfile, if I get you correctly). You need to dispatch the resetAction to your MainStack instead. This thread here suggested some ways that you can achieve this. And also, here is my preferred solution, because i don't have to pass props around navigators or calls multiple nested actions with this.

  1. Create a navigationService.js with the following contents (to keep your top level navigator as a reference)
import { NavigationActions, StackActions } from 'react-navigation';

let _navigator;

function setTopLevelNavigator(navigatorRef) {
  _navigator = navigatorRef;
}

function navigateMainNavigator(routeName, params) {
  _navigator.dispatch(
    NavigationActions.navigate({
      routeName,
      params,
    }),
  );
}

// add other navigation functions that you need and export them

export default {
  setTopLevelNavigator,
  navigateMainNavigator,
};
  1. On your App.js or any other file you render your MainStack, do this to set the reference
import NavigationService from './navigationService';

...

render() {
  return (

    ...
    <MainStack
      ref={navigatorRef => {
        NavigationService.setTopLevelNavigator(navigatorRef);
      }}
    />
    ...

  )
}
  1. And wherever when you want to reset to your AuthStack (or any other stack in your MainStack), just import NavigationService and call
NavigationService.navigateAndReset('Auth', {...yourParamsIfAny});
// 'Auth' because you named it that way in your 'MainStack'

===========================================================================

Edited

Previous solution, in navigationService.js, is for StackNavigator as the MainStack

function navigateAndReset(routeName, params) {
  _navigator.dispatch(
    StackActions.reset({
      index: 0,
      actions: [
        NavigationActions.navigate({
          routeName,
          params,
        }),
      ],
    })
  );
}
Sign up to request clarification or add additional context in comments.

7 Comments

hey wicky, sounds like great way to do that! Im using with mobx so I did it in my store, I copy part of my code here, you can see pastebin.com/KtnMAXYy I also printed the ref to see what's it include and see all his properties. but I still got same error.. maybe I miss something..
Hey Adir, I am not familiar with mobx. But try doing a conosle.log(this.main_navigation_ref) on your resetMainStack function, and check if you're on the correct navigator. Try looking for the navigator's routes, this image might help
you can see the console.log here imgur.com/a/tWhfMHA it show me the mainStack
Ahh, I just realise your MainStack is actually a SwitchNavigator. In that case, you dont need the StackActions.reset action. Just the NavigationActions.navigate will do. I have updated my answer above
exactly what I need!!!! what the difference between stack actions.reset to NavigationActions?
|
1

Try by setting it to AppStack, because anyhow it is going to redirect to GeneralStack as you have it as initialRouteName inside AppStack

const resetAction = StackActions.reset({
      index: 0,
      key: null,
      actions: [NavigationActions.navigate({ routeName: 'App' })],
    });
    this.props.navigation.dispatch(resetAction);

7 Comments

What do u mean? I want to reset from userProfile of tabstack to screenA of authstack.. how to do that?
But in question you are trying to reset it to GeneratStack instead of AuthStack
I try to do some resets, the last one i ask you is to implement logout.. any idea?
In that case you can directly reset it to Auth
I tried to reset from UserProfile of tabStack to screenA of AuthStack with this code pastebin.com/hvJ7Mp7b . I get the same error "error:error:there is no route defined for key Auth. must be one of 'TabStack,...."
|
0

You can do the following, to reset to authStack,

create a reset actions as following,

const resetAction = StackActions.reset({
  index: 0,
  actions: [NavigationActions.navigate({ routeName: "AuthStack" })],
  key: null
});
this.props.navigation.dispatch(resetAction);

and also add the AuthStack into appStack, or the stack from which you are calling the above code.

For example if your calling this from your appstack, add the following line as a route within your app stack

  const AppStack = createStackNavigator(
  {
    TabStack,
    SearchResult,
    BusinessDetail,
    BusinessMap,
    MakeAppointment,
    TermsAndConditions,
    AuthStack <---Insert THIS
  },

This works for me when using to signout.

1 Comment

I know this kind of way but I dont think this right way.. becauae if i want to reset in more complex stacks so i need always declare it in same level of stack...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.