[英]React native custom navigation header not shown
I'm trying to use a custom navigation header but it isn't showing.我正在尝试使用自定义导航标题,但未显示。 My codes are as follow :
我的代码如下:
App.js应用程序.js
<View style={styles.appWrapper}>
<AppRoute />
</View>
AppRoute
is calling my route file route.js
. AppRoute
正在调用我的路由文件route.js
。
routes.js路由.js
const PublicDrawer = createDrawerNavigator({
'DashboardScreen': {screen: Dashboard}, // the screen where I want to show custom navbar
'TaskDetailScreen': {screen: TaskDetail},
'StartTaskScreen': {screen: StartTask}
}, {
contentComponent: MainDrawer,
drawerWidth: 300,
initialRouteName: 'DashboardScreen' ,
drawerPosition: 'left'
});
const Navigation = createStackNavigator({
'MainRoutes': { screen: PublicDrawer, navigationOptions: { headerShown: false }},
});
Dashboard.js仪表板.js
export default class Dashboard extends Component<Props> {
static navigationOptions = ({ navigation }) => {
return {
header: <AppNavHeader navigation={navigation}/> // AppNavHeader from another file
}
};
constructor(props){
super(props);
this.state = {};
}
// The rest of the code
}
Dependencies依赖关系
"@react-native-community/masked-view": "^0.1.6",
"@react-navigation/stack": "^5.0.0",
"react-native": "0.60.5",
"react-navigation": "^4.1.1",
"react-navigation-drawer": "^2.3.4",
"react-navigation-stack": "^2.1.1"
If I display AppNavHeader
directly from routes.js
it is working but I don't want to call it from there for some reason.如果我直接从
routes.js
显示AppNavHeader
它正在工作,但由于某种原因我不想从那里调用它。
I'll tell you how ive implemneted the same.我会告诉你我是如何实现相同的。 I dont trust react navigation custom headers.
我不相信反应导航自定义标题。 So what i did is first ive disabled the header of react navigation by :
所以我首先通过以下方式禁用了反应导航的标题:
{
defaultNavigationOptions: {
headerShown: false,
},
initialRouteName: 'HomeCard',
transitionConfig: () => fromRight(),
},
in your stack navigator.在您的堆栈导航器中。
Now next thing is ive created a header global component that can be used all over like :现在下一步是我创建了一个标题全局组件,可以像下面一样使用:
export default class Header extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<View style={headerStyles.newHeaderStyle}>
<View style={{opacity: 0}}>
<Image source={require('../assets/images/crossIcon.png')} />
</View>
<View style={{flexShrink: 1}}>
<Text style={headerStyles.headerText}>{this.props.title}</Text>
</View>
<View style={{left: -20, paddingLeft: 10, marginLeft: 10}}>
<TouchableOpacity
style={headerStyles.imageView}
hitSlop={{top: 20, bottom: 20, left: 50, right: 50}}
onPress={() => this.props.navigation.goBack()}>
<Image
style={{height: 15, width: 15}}
source={require('../assets/images/crossIcon.png')}
/>
</TouchableOpacity>
</View>
</View>
);
}
}
and then in whatever page i want this header, i just do :然后在我想要这个标题的任何页面中,我只做:
import Header from '../components/Header';从'../components/Header'导入标题;
class Home extends Component {
render() {
return (
<SafeAreaView style={{flex: 1}}>
<Header navigation={this.props.navigation} title="SUPPORT" />
<ScrollView style={supportStyles.view1}>
{this.renderSupportData()}
</ScrollView>
</SafeAreaView>
);
}
}
Hope it helps.希望能帮助到你。 feel free for doubts
随意怀疑
i use it like this.我是这样用的。 Its work.
这是工作。 You can try
你可以试试
static navigationOptions = ({ navigation }) => { return Header(navigation, enter code here
); static navigationOptions = ({ navigation }) => { return Header(navigation,
enter code here
); } }
return { headerTitle: ({title.toUpperCase()}), headerLeft: _leftButton, headerRight: _rightButton } return { headerTitle: ({title.toUpperCase()}), headerLeft: _leftButton, headerRight: _rightButton }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.