我有简单的 DrawerNavigator ,包含多个StackNavigator 。
我的问题是,我已经设置了' navigationOptions'对于所有' StackNavigator'
都是一样的const DrawerMainRoutes = DrawerNavigator({
Stack1: { screen: Stack1 }
Stack2: { screen: Stack2 }
Stack3: { screen: Stack4 }
})
const Sack1 = StackNavigator({
Stack1: {
screen: View
},
navigationOptions: {
headerStyle: 'Blue',
headerTitleStyle: { color: '#fff', },
headerTintColor: '#fff',
headerLeft: <MenuIcon />
}
})
是否有更好的方法为每个StackNavigator设置此 navigationOptions
答案 0 :(得分:1)
navigationOptions
应该在一个单独的变量中定义,然后你可以重复使用它:
const navigationOptions = {
headerStyle: 'Blue',
headerTitleStyle: { color: '#fff', },
headerTintColor: '#fff',
headerLeft: <MenuIcon />
}
const Sack1 = StackNavigator({
Stack1: {
screen: View
},
navigationOptions
})
const Sack2 = StackNavigator({
Stack2: {
screen: View
},
navigationOptions
})
...
或者您可以创建StackNavigator创建者函数:
const createStackNavigator = (routeOptions) =>
StackNavigator(routeOptions, {
navigationOptions: {
headerStyle: 'Blue',
headerTitleStyle: { color: '#fff', },
headerTintColor: '#fff',
headerLeft: <MenuIcon />
})
const Stack1 = createStackNavigator({ Stack1: { screen: View } })
const Stack2 = createStackNavigator({ Stack2: { screen: View } })
....