如果您的stack view的路由索引不为 0 ,关键是在您的路由方法中调度正确的导航操作。tabBarOnPress
index.js
import React, { AppRegistry } from 'react-native'
import { Component } from 'react'
import MyTabNavigator from './components/MyTabNavigator'
class MyApp extends Component {
render() {
return (
<MyTabNavigator/>
);
}
}
AppRegistry.registerComponent('MyApp', () => MyApp);
MyTabNavigator.js
import HomeStackNav from './HomeStackNav'
import SettingsStackNav from './SettingsStackNav'
const goBackNavigationAction = NavigationActions.navigate({
action: NavigationActions.back()
})
const routeConfig = {
Home: {
screen: HomeStackNav,
path: 'home',
navigationOptions: {...}
},
Settings: {
screen: SettingsStackNav,
path: 'settings',
navigationOptions: ({ navigation }) => ({
title: 'settings',
tabBarLabel: 'settings',
tabBarOnPress: (scene, jumpToIndex) => {
jumpToIndex(scene.index)
if (scene.route.index > 0) { // <----- this is
navigation.dispatch(goBackNavigationAction) // <----- where the
} // <----- magic happens
}
})
}
}
const tabNavigatorConfig = {...}
export default TabNavigator(routeConfig, tabNavigatorConfig)