使用抽屉导航器嵌套堆栈导航器

时间:2017-10-14 16:16:57

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

我正在尝试使用抽屉导航进行堆叠导航。基本上我希望抽屉一直只在一个场景中。我试过这样做但没有任何事情发生。我无法理解我做错了什么。

P.S:我是反应原生的新手。

ConversationListScreen.js

import React, { Component } from 'react';
import {
    View,
    Text,
    Button,
    TouchableNativeFeedback,
    Alert,
} from 'react-native';
import Icon from 'react-native-vector-icons/MaterialIcons';
import styles from './styles';

export default class ConversationListScreen extends Component
{
    static navigationOptions = {
        title: 'Hola',
        headerLeft: (
            <TouchableNativeFeedback onPress={() => Alert.alert('Hi!', 'I am a hamburger.')}>
                <View style={styles.toolBackground}>
                    <Icon name="menu" style={ styles.menuIcon }/>
                </View>
            </TouchableNativeFeedback>
        ),
        headerRight: (
            <TouchableNativeFeedback onPress={() => Alert.alert('Hi!', 'What you want to search?')}>
                <View style={styles.toolBackground}>
                    <Icon name="search" style={ styles.searchIcon }/>
                </View>
            </TouchableNativeFeedback>
        )
    };
    render() {
        return (
            <View>
            </View>
        );
    }
}

Drawer.js

import React, { Component } from 'react';
import {
    View,
    Text,
} from 'react-native';

export default class SideNav extends Component
{
    render() {
        return (
            <View>
                <Text>My first Drawer</Text>
            </View>
        );
    }
}

RouteConfig.js

import { StackNavigator, DrawerNavigator } from 'react-navigation';
import ConversationListScreen from './ConversationListScreen';
import Drawer from './Drawer';
import ChatScreen from './ChatScreen';

export const SideNav = DrawerNavigator({
    Drawer: { screen: Drawer},
});

export const Hola = StackNavigator({
    ConversationList: { screen: ConversationListScreen },
    Drawer: { screen: SideNav },
    Chat: { screen: ChatScreen },
});

4 个答案:

答案 0 :(得分:1)

我有一个类似的问题,我最终做的是使用this drawer component并将其包装在您想要访问它的组件周围。例如,如果您只想从ConversationListScreen访问它,则将其导出为<Drawer><ConversationListScreen></Drawer>

答案 1 :(得分:1)

嘿你可以尝试在DrawerNavigator中使用StackNavigator来支持两种类型的导航。请看这个例子 -

http://androidseekho.com/others/reactnative/how-to-implement-drawer-navigator-in-react-native/

答案 2 :(得分:0)

我猜你不能在堆栈导航器中使用抽屉导航器作为场景。您应该在要使用的组件中导入SideNav并尝试将其作为组件呈现

答案 3 :(得分:0)

这是一种解决方法。我们在HomeScreenNavigator中有两个屏幕,它是一个stackNavigator。在firstScreen中我们有一个指向secondScreen的链接。在secondScreen中我们有drawerNavigator,它有一个名为ComponentWidthDrawer的屏幕。这个组件有一个打开抽屉的按钮。我测试了它,它正在工作。

class FirstComponent extends Component{
  render () {
    return (
      <View>
        <TouchableOpacity onPress={() => 
this.props.navigation.navigate("SecondScreen")}>
          <Text>Go to Second Component</Text>
        </TouchableOpacity>
      </View>
    )
  }
}
class ComponentWidthDrawer extends Component{
  render () {
    return (
      <View>
        <TouchableOpacity onPress={() => this.props.navigation.navigate("DrawerOpen")}>
          <Text>Open Menu</Text>
        </TouchableOpacity>
      </View>
    )
  }
}
const SecondComponent = DrawerNavigator({
  Drawer: {
    screen: ComponentWidthDrawer,
    navigationOptions: {
      drawerLabel: 'Videos'
    },
  }
})

const HomeScreenNavigator = StackNavigator(
  {
    FirstScreen : {
      screen: FirstComponent,
      navigationOptions:{
        header: false
      }
    },
    SecondScreen: {
      screen: SecondComponent,
      navigationOptions:{
        header: false
      }
    }
  }
);