tabBarOnPress 在反应本机的反应导航选项卡中不起作用
tabBarOnPress not working in react navigation tabs in react-native
我正在使用嵌套导航抽屉 > 选项卡 > 堆栈导航器。
导航器正在工作,但是当我点击选项卡项时它抛出以下错误
undefined is not an object (evaluating '_ref4.route')
_getOnPress
TabView.js:110:44
_handleOnPress
TabBarTop.js:127:31 onPress
TabBar.js:466:33
_callTimer
JSTimers.js:156:15 callTimers
JSTimers.js:411:17
__callFunction
MessageQueue.js:302:47
MessageQueue.js:116:26
__guard
MessageQueue.js:265:6 callFunctionReturnFlushedQueue
MessageQueue.js:115:17
我被困在这里我尝试了很多但没有成功
这是我的代码
DrawerNav.js
import React, { Component } from 'react';
import { DrawerNavigator} from 'react-navigation';
import Icon from "react-native-vector-icons/FontAwesome";
import stackNav from './stackNav';
const Drawer = DrawerNavigator({
DrawerItem1: {
screen: stackNav,
navigationOptions: {
drawerLabel: "Drawer Item 1",
drawerIcon: ({ tintColor }) => <Icon name="rocket" size={24} />
},
},
DrawerItem2: {
screen: stackNav,
navigationOptions: {
drawerLabel: "Drawer Item 2",
drawerIcon: ({ tintColor }) => <Icon name="rocket" size={24} />
},
}
});
export default Drawer;
TabNav.js
import React, { Component } from 'react';
import { TabNavigator, TabView } from 'react-navigation'
import Icon from "react-native-vector-icons/FontAwesome";
import MainScreen from './screens/MainScreen';
const tabNav = TabNavigator({
TabItem1: {
screen: MainScreen,
navigationOptions: {
tabBarLabel:"Tab One",
tabBarIcon: ({ tintColor }) => <Icon name={"glass"} size={30} color={tintColor} />
}
},
TabItem2: {
screen: MainScreen,
navigationOptions: {
tabBarLabel:"Tab Two",
tabBarIcon: ({ tintColor }) => <Icon name={"glass"} size={30} color={tintColor} />
}
},
TabItem3: {
screen: MainScreen,
navigationOptions: {
tabBarLabel:"Tab Three",
tabBarIcon: ({ tintColor }) => <Icon name={"glass"} size={30} color={tintColor} />
}
}
///... add more tabs here
}, {
tabBarPosition: 'bottom',
tabBarOptions: {
activeTintColor: '#222'
},
});
export default tabNav;
stackNav.js
import React, { Component } from 'react';
import { TouchableOpacity } from 'react-native';
import { StackNavigator} from 'react-navigation'
import IOSIcon from "react-native-vector-icons/Ionicons";
import DetailScreen from './screens/DetailScreen';
import MainScreen from './screens/MainScreen';
import tabNav from './tabNav'
const stackNav = StackNavigator({
Main: {
screen: tabNav,
navigationOptions:({navigation}) => ({
title: "Main",
headerLeft:(
<TouchableOpacity onPress={() => navigation.navigate("DrawerOpen")}>
<IOSIcon name="ios-menu" size={30} />
</TouchableOpacity>
),
headerStyle: { paddingRight: 10, paddingLeft: 10 },
})
},
Detail: {
screen: DetailScreen,
navigationOptions: (props) => ({
title: "Detail",
})
}
})
export default stackNav;
我在使用 TabNavigator 时遇到同样的错误。
我也执行了官方的反应导航示例(https://github.com/react-community/react-navigation/blob/master/examples/NavigationPlayground/js/SimpleTabs.js)但没有成功(同样的错误)。
我猜这是 Expo/React Native 最新版本的错误。
您使用的是什么版本?
我正在使用:
"dependencies": {
"expo": "^22.0.2",
"react": "16.0.0-beta.5",
"react-native": "^0.49.5",
"react-navigation": "^1.0.0-beta.20"
}
已确认:从Create React Native App中弹出解决了问题。
所以,这似乎是 Create React Native App 或 Expo 上的一个错误。
我正在使用嵌套导航抽屉 > 选项卡 > 堆栈导航器。
导航器正在工作,但是当我点击选项卡项时它抛出以下错误
undefined is not an object (evaluating '_ref4.route') _getOnPress TabView.js:110:44 _handleOnPress TabBarTop.js:127:31 onPress TabBar.js:466:33 _callTimer JSTimers.js:156:15 callTimers JSTimers.js:411:17 __callFunction MessageQueue.js:302:47 MessageQueue.js:116:26 __guard MessageQueue.js:265:6 callFunctionReturnFlushedQueue MessageQueue.js:115:17
我被困在这里我尝试了很多但没有成功
这是我的代码
DrawerNav.js
import React, { Component } from 'react';
import { DrawerNavigator} from 'react-navigation';
import Icon from "react-native-vector-icons/FontAwesome";
import stackNav from './stackNav';
const Drawer = DrawerNavigator({
DrawerItem1: {
screen: stackNav,
navigationOptions: {
drawerLabel: "Drawer Item 1",
drawerIcon: ({ tintColor }) => <Icon name="rocket" size={24} />
},
},
DrawerItem2: {
screen: stackNav,
navigationOptions: {
drawerLabel: "Drawer Item 2",
drawerIcon: ({ tintColor }) => <Icon name="rocket" size={24} />
},
}
});
export default Drawer;
TabNav.js
import React, { Component } from 'react';
import { TabNavigator, TabView } from 'react-navigation'
import Icon from "react-native-vector-icons/FontAwesome";
import MainScreen from './screens/MainScreen';
const tabNav = TabNavigator({
TabItem1: {
screen: MainScreen,
navigationOptions: {
tabBarLabel:"Tab One",
tabBarIcon: ({ tintColor }) => <Icon name={"glass"} size={30} color={tintColor} />
}
},
TabItem2: {
screen: MainScreen,
navigationOptions: {
tabBarLabel:"Tab Two",
tabBarIcon: ({ tintColor }) => <Icon name={"glass"} size={30} color={tintColor} />
}
},
TabItem3: {
screen: MainScreen,
navigationOptions: {
tabBarLabel:"Tab Three",
tabBarIcon: ({ tintColor }) => <Icon name={"glass"} size={30} color={tintColor} />
}
}
///... add more tabs here
}, {
tabBarPosition: 'bottom',
tabBarOptions: {
activeTintColor: '#222'
},
});
export default tabNav;
stackNav.js
import React, { Component } from 'react';
import { TouchableOpacity } from 'react-native';
import { StackNavigator} from 'react-navigation'
import IOSIcon from "react-native-vector-icons/Ionicons";
import DetailScreen from './screens/DetailScreen';
import MainScreen from './screens/MainScreen';
import tabNav from './tabNav'
const stackNav = StackNavigator({
Main: {
screen: tabNav,
navigationOptions:({navigation}) => ({
title: "Main",
headerLeft:(
<TouchableOpacity onPress={() => navigation.navigate("DrawerOpen")}>
<IOSIcon name="ios-menu" size={30} />
</TouchableOpacity>
),
headerStyle: { paddingRight: 10, paddingLeft: 10 },
})
},
Detail: {
screen: DetailScreen,
navigationOptions: (props) => ({
title: "Detail",
})
}
})
export default stackNav;
我在使用 TabNavigator 时遇到同样的错误。
我也执行了官方的反应导航示例(https://github.com/react-community/react-navigation/blob/master/examples/NavigationPlayground/js/SimpleTabs.js)但没有成功(同样的错误)。
我猜这是 Expo/React Native 最新版本的错误。
您使用的是什么版本?
我正在使用:
"dependencies": {
"expo": "^22.0.2",
"react": "16.0.0-beta.5",
"react-native": "^0.49.5",
"react-navigation": "^1.0.0-beta.20"
}
已确认:从Create React Native App中弹出解决了问题。 所以,这似乎是 Create React Native App 或 Expo 上的一个错误。