Home > Back-end >  How to reset the first screen in a navigator?
How to reset the first screen in a navigator?

Time:03-10

In my react native app I am navigating from one navigator to a screen in another navigator like this

navigation.navigate(Screens.EXPLORE, {
                  screen: Screens.BRIEF,
                  params: {
                    briefId: briefInfo.campaign_id,
                  },
                });

However once I do this and tap the back button and which returns me the the screen before in its own navigator, if I try to navigate to the other navigator by clicking the icon which should navigate to the home screen of that navigator, it navigates still to the screen I navigated before, within the navigator. Is it possible in any way that when I click the back button in the screen to reset the navigator so that when I normally navigate to it again, it shows me the first screen within the navigator?

CodePudding user response:

import { CommonActions } from '@react-navigation/native';

navigation.dispatch(
  CommonActions.navigate({
    name: 'Profile',     // your screen name
    params: {
      user: 'jane',     //your params
    },
  })
);

For reference:- https://reactnavigation.org/docs/navigation-actions/#navigate

CodePudding user response:

@this.arjun answer did not help. I managed to fix my problem by changing my above code to this

navigation.navigate(Screens.EXPLORE, {
                  screen: Screens.BRIEF,
                  initial: false,
                  params: {
                    briefId: briefInfo.campaign_id,
                    withPop: true,
                  },
                });

Then in the screen I was navigating to I added this code instead of just doing navigation.goBack()

I added the withPop conditional logic.

onPress={() => {
                        if (withPop) {
                          navigation.pop();
                        }
                        navigation.goBack();
                      }}

I am still missing an animation when I am going back from that screen. But I can live with that.

  • Related