2

I have the following structure in my React Native project, suggested by this answer:

export const Root = () => {
    const RootStack = useMemo(() => createStackNavigator(), []);
    const HomeStack = useMemo(() => createStackNavigator(), []);

    function renderHomeStack() {
        return (
            <HomeStack.Navigator>
                <HomeStack.Screen name="Home" component={HomeScreen} />
                <HomeStack.Screen name="Settings" component={SettingsScreen} />
            </HomeStack.Navigator>
        );
    }

    function renderRootStack() {
        return (
            <RootStack.Navigator>
                <RootStack.Screen name="Login" component={LoginScreen} />
                <RootStack.Screen
                    name="HomeStack"
                    options={{
                        presentation: 'modal',
                    }}
                    component={renderHomeStack}
                />
            </RootStack.Navigator>
        );
    }

    return renderRootStack();
};

Currently, I want to go back from the Settings screen to the Login screen.

  • I tried the solutions under this question, but they didn't work.

  • I also tried using DeviceEventEmitter to send an event to the login screen to call navigation.popToTop() or navigation.goBack() - that didn't work either.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223

3 Answers3

0

Try this, you have to pass the parent props before,

props.navigation.navigate("LoginScreen");

or you can use AuthContext to save it in one location and goback,

      const initialLoginstate = {
        isLoading: true,
        userName: null,
        userToken: null,
      };

      const loginReducer = (prevState, action) => {
        switch (action.type) {
    
          case "LOGOUT":
            return {
              ...prevState,
              userName: null,
              userToken: null,
              isLoading: false,
            };
        }
      };

 
    const [loginState, dispatch] = React.useReducer(
        loginReducer,
        initialLoginstate
      );

 const authContext = React.useMemo(
        () => ({
    signOut: async () => {
    
                try {
                  await AsyncStorage.removeItem("userToken");
                } catch (e) {
                  console.log(e);
                }
                dispatch({ type: "LOGOUT" });
        }),
        []
      );
fixedDrill
  • 183
  • 1
  • 13
0

Try using

 navigation.reset({
  index: 0,
  routes: [{ name: 'Login' }],
});
Xhirazi
  • 735
  • 4
  • 15
  • This doesn't work, as the navigation reference on the Settings screen doesn't know about the login screen. I need a way to access that other navigation reference somehow. – Tamás Sengel Jan 20 '22 at 14:24
0

Option 1:

You just need to call navigate and pass in the id of the screen you want to navigate to.

i.e. The screen you want to see when the modal is closed:

props.navigation.navigate("MyScreen");

This will close the modal (as well as any of the screens that were in the modal).

Option 2:

props.navigation.popToTop();
props.navigation.goBack(null);
Reece Kenney
  • 2,734
  • 3
  • 24
  • 57