Search code examples
javascriptreact-nativereact-navigationstack-navigator

StackNavigator - skipping one page the page in the stack


In my React Native application, I have used React Navigation library for implementing navigation. I built the whole application upon StackNavigator. Now I ran into a problem where I don't know how to solve. Sometimes based on some condition, I want to skip one of the pages in my stack navigator.

For example, I have four screens.

const GuidelinesNavigation = StackNavigator({
  'ScreenOneRoute': { screen: ScreenOne },
  'ScreenTwoRoute': { screen: ScreenTwo },
  'ScreenThreeRoute': { screen: ScreenThree },
  'ScreenFourRoute': { screen: ScreenFour }
});

and I want to skip the third screen based on the decision that I make on the second screen, and I don't know how to.


Solution

  • You can navigate between these pages however you want to, e.g. Suppose you have a button in ScreenOne, and on press that button, you want to take user to ScreenThree, you can do something like this:

    <TouchableOpacity onPress={() => this.props.navigation.navigate('ScreenThree')}>
        <Text>
            Go to Screen 3
        </Text>
    </TouchableOpacity>
    

    If you have another button that goes to ScreenTwo, do exactly the same thing but change 'ScreenThree' to ScreenTwo