Navigate to root screen from nested stack navigator

后端 未结 4 1152
迷失自我
迷失自我 2021-01-18 21:59

i am new to react and trying to learn it by myself , i am facing problem in navigating user back to root screen from nested stck navigator screen .

Here is some of m

4条回答
  •  不思量自难忘°
    2021-01-18 22:22

    Modal StackNavigator containing a Dismissable StackNavigator

    Requires: react-navigation version: 1.0.0

    Goal: Navigate from App TabNavigator to Screen 1 to Screen 2 to Screen N and then directly back to App TabNavigator.

    Navigation hierarchy:

    • RootNavigator StackNavigator {mode: 'modal'}
      • App TabNavigator
        • TabA Screen
        • TabB Screen
        • TabC Screen
      • ModalScreen Screen
      • ModalStack DismissableStackNavigator
        • Screen 1 ModalStackScreen
        • Screen 2 ModalStackScreen
        • Screen N ModalStackScreen

    Demo

    package.json

    {
      "name": "HelloWorld",
      "version": "0.0.1",
      "private": true,
      "scripts": {
        "start": "node node_modules/react-native/local-cli/cli.js start",
        "test": "jest"
      },
      "dependencies": {
        "react": "16.0.0-alpha.6",
        "react-native": "0.44.0",
        "react-navigation": "^1.0.0"
      },
      "devDependencies": {
        "babel-jest": "20.0.3",
        "babel-preset-react-native": "1.9.2",
        "jest": "20.0.3",
        "react-test-renderer": "16.0.0-alpha.6"
      },
      "jest": {
        "preset": "react-native"
      }
    }
    

    index.ios.js (or index.android.js)

    import React from 'react'
    import {
      AppRegistry,
      Button,
      Text,
      View
    } from 'react-native'
    import {
      StackNavigator,
      TabNavigator
    } from 'react-navigation'
    
    class TabA extends React.Component {
      state = {
        startScreen: 'none',
        returnScreen: 'none'
      }
      render () {
        return (
          
            {this.constructor.name}
            startScreen: {this.state.startScreen}
            returnScreen: {this.state.returnScreen}
            

    Previous Answer

    This solution is a sledgehammer. It causes the screen of the default tab in the TabNavigator to unmount and then mount again. If the Screen a tab launching the Modal with the StackNavigator passes some callbacks to update it's state, these callbacks will be called when the Screen is unmounted.

    The solution was to add key: null to the reset object:

    this.props.navigation.dispatch(NavigationActions.reset({
      index: 0,
      key: null,
      actions: [
        NavigationActions.navigate({ routeName: 'App'})
      ]
    }))
    

    I was tipped off by this comment.

    (FYI - I got here via your comment asking for help.)

提交回复
热议问题