渲染项目屏幕后,React-Navigation抽屉立即将我路由回上一屏幕

时间:2018-11-21 06:33:27

标签: react-native react-navigation react-navigation-stack react-navigation-drawer

我有这样的StackNavigation:

const AppNavigator = createStackNavigator({

Login: {
    screen: Login,
    navigationOptions: () => ({
        title: 'Login',
        headerTintColor: 'white',
        headerStyle:{
            backgroundColor: '#000',
            elevation: 0,
            showdowOpacity: 0
        },
    })
},

Home: {
    screen: AppDrawerNavigator,
    navigationOptions: () => ({
        header: null
    })
},
});

其中内置了DrawerNavigator:

const AppDrawerNavigator = createDrawerNavigator({

Home: {
    screen: Home,
    navigationOptions: {
        drawerLabel: 'Home',
        gesturesEnabled: false,
    }
},

Favorites: {
    screen: Favorites,
    navigationOptions: {
        drawerLabel: 'Favorites',

    }
}

},
{
    drawerPosition: 'left',
    contentComponent: props => <Drawer {...props} />
});

堆栈导航器的初始路由运行正常

  

登录->主页

但是当我尝试从“主页”导航到“收藏夹”时,呈现“收藏夹”屏幕后会立即导航回到“主页”。

我正在使用react-navigation@2.11.2和react-native@0.56.0

1 个答案:

答案 0 :(得分:1)

在堆栈导航器和抽屉导航器中都使用Home。 这里极有可能发生名称冲突。

尝试这种结构。

const Stack = {
    FirstView: {
        screen: FirstView
    },
    SecondView: {
        screen: SecondView
    },
    ThirdView: {
        screen: ThirdView
    }
};

const DrawerRoutes = {
    FirstViewStack: {
        name: 'FirstViewStack',
        screen: StackNavigator(Stack, { initialRouteName: 'FirstView' })
    },
    SecondViewStack: {
        name: 'SecondViewStack',
        screen: StackNavigator(Stack, { initialRouteName: 'SecondView' })
    },
    ThirdViewStack: {
        name: 'ThirdViewStack',
        screen: StackNavigator(Stack, { initialRouteName: 'ThirdView' })
    },
};

const RootNavigator =
    StackNavigator({
        Drawer: {
            name: 'Drawer',
            screen: DrawerNavigator(
                DrawerRoutes,
            ),
        },
        ...Stack
    },
        {
            headerMode: 'none'
        }
    );

当我尝试在我的主页(使用堆栈导航器转到其他页面)中使用汉堡菜单时,遇到了类似的问题。

也请选中此Git Article