我正在使用 TabNavigator 和 StackNavigator 构建React Native应用。
让我们假设我在设置标签上,我在用户设置中深入了3个屏幕 - >付款选项 - >信用卡1 。
每次点击设置标签,我想返回一个屏幕。
我该怎么做?
答案 0 :(得分:0)
如果你的堆栈视图的路由索引是,那么关键是在路由的tabBarOnPress
方法中调度正确的导航操作不是0。
<强> 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)