How to navigate between different nested stacks in react navigation

后端 未结 14 755
情话喂你
情话喂你 2020-12-02 16:52

The Goal

Using react navigation, navigate from a screen in a navigator to a screen in a different navigator.

More Detail

If I have the following

相关标签:
14条回答
  • 2020-12-02 17:31

    While working on a react-native project, i came across same situation. I have tried multiple ways in navigating to screen but failed.

    After many trials, I tried passing parents navigation object to children and made a navigation function call and it worked.

    Now coming to your issues, If you want to navigation from screen D to screen A do follow these steps.

    -> Pass nested navigator 2 navigation props to its children using screenProps.

    export default class Home extends Component {
        static navigationOptions = {
            header:null
        };
    
        constructor(props) {
            super(props);
            this.state = {
                profileData: this.props.navigation.state.params,
                route_index: '',
            }
        }
    
        render() {
            return (
                <ParentNavigator screenProps={this.props.navigation} />
            );
        }
    }
    
    export const ParentNavigator = StackNavigator({
      // ScreenName : { screen : importedClassname }
      Nested1: { screen: nested1 },
      Nested2: { screen : nestes1 }
    });
    
    export const nested1 = StackNavigator({
      ScreenA: { screen: screenA },
      ScreenB: { screen : screenB }
    });
    
    export const nested2 = StackNavigator({
      ScreenC: { screen: screenC },
      ScreenD: { screen : screenD }
    });
    

    You can receive the navigation in children using

    const {navigate} = this.props.screenProps.navigation;
    

    Now this navigate() can be used to navigate between children.

    I accept that this process is little confusing but i couldn't find any solutions so had to go with this as i have to complete my requirement.

    0 讨论(0)
  • 2020-12-02 17:32

    try this,

    Parent Navigator
      Nested Navigator 1
        screen A
        screen B
      Nested Navigator 2
        screen A
        screen C
        screen D
    

    and then, there is no need to go A in 1 from D in 2, you can just go A from D both in 2, you can check here image or A stack navigator for each tab

    0 讨论(0)
提交回复
热议问题