'How to reset the first screen in a navigator?

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?



Solution 1:[1]

Hey if you are using @react-navigation/native then just import it in your component like this

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

const navigation = useNavigation();

navigation.reset({
  index: 0,
  routes: [{ name: 'Profile' }] 
})

it will reset your stacks and also you can you replace instead of navigate that will replace current to new screen

navigation.replace('LoginScreen')

I'm just putting Image over here for your reference. enter image description here

Solution 2:[2]

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

Solution 3:[3]

@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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sumit_VE
Solution 2 this.arjun
Solution 3 Talmacel Marian Silviu