我正在尝试将堆栈导航与抽屉导航一起使用。基本上我希望抽屉一直只在一个场景中。我试图这样做,但没有任何反应。我无法理解我做错了什么。
PS:我是新手反应原生。
对话列表屏幕.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>
);
}
}
抽屉.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>
);
}
}
路由配置.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 },
});
我遇到了类似的问题,我最终所做的是使用此抽屉组件并将其包裹在您要用来访问它的组件周围。例如,如果您只想从对话列表屏幕访问它,则将其导出为<Drawer><ConversationListScreen></Drawer>
我想您不能在堆栈导航器中使用抽屉导航器作为场景。 您应该在要使用的组件中导入 SideNav,并尝试将其渲染为组件
这是一个解决方法。我们在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
}
}
}
);