android - 来自另一个文件的 DrawerNavigator 不起作用
问题描述
我在 App.js 中有以下代码
import React, { Component } from 'react';
import { Text, View,} from 'react-native';
import{DrawerNavigator, DrawerActions} from 'react-navigation';
import { Menu} from './src/components/menu';
export default class MainView extends Component {
render(){
return(
<View>
<Menu />
<Text> WHAT ??? </Text>
</View>
);
}
}
以及 src/components/menu.js 中的以下代码
'use strict';
import React, { Component } from 'react';
import { Text, View, StyleSheet, Image, ScrollView} from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome5';
import{DrawerNavigator, DrawerActions} from 'react-navigation';
export class Menu extends Component {
render(){
return(
<View style= {styles.navContainer}>
<View style= {styles.navContainerFlexing}>
<View>
<Icon name="bars" size={25} color= 'black' style={{marginLeft: 10, fontWeight: '200' }} onPress={() => this.props.navigation.dispatch(DrawerActions.toggleDrawer())} />
</View>
<Text style={[styles.whiteText, styles.navItem]}>Home</Text>
</View>
</View>
);
}
}
export const Drawer = DrawerNavigator(
{
Menu: Menu,
},
{
// initialRouteName: 'Home',
},
{
drawerPosition: 'left',
initialRouteName: 'Home',
drawerBackgroundColor: 'white',
drawerWidth: 300,
}
);
const styles= StyleSheet.create({
navContainer: {
height: 55,
backgroundColor: '#3ba558',
alignItems: 'center',
// flex: 1,
flexDirection: 'row',
// justifyContent: 'flex-start'
},
navContainerFlexing: {
flex: 2,
flexDirection: 'row',
backgroundColor: '#3ba558',
paddingLeft: 20
},
whiteText: {
color: 'white',
},
navItem: {
alignItems: 'center',
marginTop: 'auto',
marginBottom: 'auto',
marginLeft: 10
},
});
现在我希望我的 Menu 类显示在 App.js 中,它正在显示,但我也希望它在主页中可用 DrawerNavigator,现在抽屉正在提供:
undefined is not an object (evaluating '_this.props.navigation.dispatch')
解决方案
我已经解释了下面的DrawerNavigator
配置link
。
在下面查看接受的答案link
。
正如我在其中解释的那样,尝试理解这个概念,不要复制语法。
和你的配置做个对比,你会发现你的问题。
推荐阅读
- flutter - 使用颤振,每次我重新加载 ListView 时,我的带有 MemoryImage 的 CircleAvatar 都会闪烁
- r - 使用 ggplot2 和 log2_trans() 进行 log_2 缩放的问题
- mysql - 如果不为空,Mysql 会合并列
- django - 表单验证未显示在表单上
- java - 外键必须与引用的主键错误具有相同的列数,但没有具有复合键的实体
- python - TensorFlow v2:替换 tf.contrib.predictor.from_saved_model
- powershell - Shell 脚本 - 在现有 xls 文件上设置自动过滤器
- javascript - 我创建了一个非常简单的井字游戏,我在使用 jquery 进行索引和获胜检查时遇到了问题
- python - 如何根据条件删除python有序字典中的子键、值对?
- applescript - 如何在 AppleScript 中获取/设置文件夹而不是窗口的视图选项?