嵌套Drawer-StackNavigator,触发“DrawerOpen”不能使用React-Native

时间:2017-12-13 14:00:12

标签: react-native react-navigation

我正在使用React Native和react-navigation制作应用程序。

[我的应用程序的层次结构]

*抽屉(APP)

ㄴStackNavigator

ㄴStackNavigator

我想要的是火navigation.navigate('DrawerOpen');

我可以通过从左边缘拖动来显示我的抽屉,但不是通过按导航标题左侧的“菜单按钮”触发它。我花了这么多次来存档这个。请帮帮我。

const Nav = StackNavigator({
    mainnav_list:{
        screen: (props) => <TodoList {...props} dbCollectionName={props.screenProps.dbCollectionName}/>,
        navigationOptions:({navigation}) => ({
            headerLeft:(
                <TouchableOpacity onPress={() => {console.log(navigation); navigation.navigate('DrawerOpen');}}>
                    <Text style={{color:'white', marginLeft:15}}>Menu</Text>
                </TouchableOpacity>
            )
        })
    },
    mainnav_detail:{screen: TodoDetail}
}
,
{
    navigationOptions:(props) => ({
        title:props.screenProps.dbCollectionName,
        headerBackTitle:null,
        headerStyle:{backgroundColor:'#000'},
        headerTitleStyle:{color:'#fff'},
        headerTintColor:'#fff',
    })
})

const AppDrawer = DrawerNavigator(
{
    drawer1:{screen:() => <Nav screenProps={{dbCollectionName:'todos'}}/> },
    drawer2:{screen:() => <Nav screenProps={{dbCollectionName:'todos2'}}/> }
})

AppRegistry.registerComponent('TodosFS', () => AppDrawer);

2 个答案:

答案 0 :(得分:6)

在新版 reactnavigation 中打开,关闭或切换您的DrawerNavigator只需使用以下

this.props.navigation.openDrawer();
this.props.navigation.closeDrawer();
this.props.navigation.toggleDrawer();

https://reactnavigation.org/docs/en/drawer-based-navigation.html

答案 1 :(得分:0)

我解决了!!

const Nav = StackNavigator({
    mainnav_list:{
        screen: (props) => <TodoList {...props} dbCollectionName={props.screenProps.dbCollectionName}/>,
        navigationOptions:(props) => ({
            headerLeft:(
                <TouchableOpacity onPress={() => {props.screenProps.drawerNavigation.navigate('DrawerOpen');}}>
                    <Text style={{color:'white', marginLeft:15, fontWeight:'bold'}}>Menu</Text>
                </TouchableOpacity>
            )
        })
    },
    mainnav_detail:{screen: TodoDetail}
}
,
{
    navigationOptions:(props) => ({
        title:props.screenProps.dbCollectionName,
        headerBackTitle:null,
        headerStyle:{backgroundColor:'#000'},
        headerTitleStyle:{color:'#fff'},
        headerTintColor:'#fff',
    })
})

const AppDrawer = DrawerNavigator(
{
    drawer1:{screen:({navigation}) => <Nav screenProps={{dbCollectionName:'todos', drawerNavigation:navigation}}/> },
    drawer2:{screen:({navigation}) => <Nav screenProps={{dbCollectionName:'todos2', drawerNavigation:navigation}}/> }
})

AppRegistry.registerComponent('TodosFS', () => AppDrawer);