我本机反应还很陌生。我正在尝试为我的应用设置一些全局标题样式,但是它不起作用
route.js
import React, { Component } from 'react';
import { View } from 'react-native';
import { createStackNavigator, createAppContainer } from "react-navigation";
import SplashScreen from '../screens/SplashScreen';
import CalendarScreeen from '../screens/CalendarScreen';
const NavStack = createStackNavigator(
{
Splash: {
screen: SplashScreen,
navigationOptions: {
header: null
},
},
Calendar: {
screen: CalendarScreeen,
navigationOptions: {
title: 'Calendar',
},
},
},
{
initialRouteName: 'Calendar',
navigationOptions: {
headerStyle: {
backgroundColor: '#28F1A6',
elevation: 0,
shadowOpacity: 0
},
headerTintColor: '#333333',
headerTitleStyle: {
fontWeight: 'bold',
color: '#ffffff'
}
}
}
);
const Routes = createAppContainer(NavStack);
export default Routes;
现在,我知道我可以在班级组件中做类似的事情
static navigationOptions = {
title: 'Chat',
headerStyle: { backgroundColor: 'red' },
headerTitleStyle: { color: 'green' },
}
但是我想从我的route.js
我也尝试过docs中提到的defaultNavigationOptions
但是没有运气!!
答案 0 :(得分:2)
要设置自定义反应反应导航,您需要先在App.js中定义 option.navigations 或 option.defaultNavigationOptions
App.js
<Stack.Screen
name="ListeMedocItemScreen"
component={ListeMedocItemScreen}
options={ListeMedocItemScreen.defaultNavigationOptions} // ListeMedocItemScreen.navigationOptions
/>
然后在您的“同意事项”页面中
如果使用钩子
const ListeMedocItem = (props) =>{
//some component
}
ListeMedocItem.defaultNavigationOptions = ({navigation}) => {
return {
title: 'My home',
headerStyle: {
backgroundColor: '#f4511e',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
}}
或者如果您使用React.Components
static navigationOptions = ({ navigation }) => {
//return header with Custom View which will replace the original header
return {
header: (
<View
style={{
height: 45,
marginTop: 20,
backgroundColor: 'red',
justifyContent: 'center',
}}>
<Text
style={{
color: 'white',
textAlign: 'center',
fontWeight: 'bold',
fontSize: 18,
}}>
This is Custom Header
</Text>
</View>
),
};
};
答案 1 :(得分:1)
您需要使用defaultNavigationOptions
。
诚然,他们甚至没有提到他们在文档的v2和v3之间进行了更改!
答案 2 :(得分:1)
只需将您的navigationOptions
更改为defaultNavigationOptions
,它将正常工作
谢谢!
答案 3 :(得分:-1)