我有以下反应本机代码:
import React from 'react';
import {AppRegistry} from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator} from 'react-navigation-stack';
import { createDrawerNavigator} from 'react-navigation-drawer';
import {View,Text,Button} from 'react-native';
class Sub extends React.Component {
static navigationOptions = {
headerTitle: () => (
<Button
onPress={() => this.props.navigation.dangerouslyGetParent().openDrawer()}
title="Open Drawer"
/>
),
};
render() {
const props = this.props;
return (
<View>
<Text>{props.navigation.state.routeName}</Text>
<Button onPress={_=>props.navigation.navigate("Sub1")} title="Sub1" />
<Button onPress={_=>props.navigation.navigate("Sub2")} title="Sub2" />
</View>
);
}
}
const SubRoutes = {
"Sub1":Sub
};
const SubNavigator = createStackNavigator(SubRoutes);
const SubApp = createAppContainer(SubNavigator);
const MainRoutes = {
"Main1":SubApp
};
const MainNavigator = createDrawerNavigator(MainRoutes);
const App = createAppContainer(MainNavigator);
AppRegistry.registerComponent("nav", () => App);
它将StackNavigator呈现为DrawerNavigator的子代。在Sub
屏幕上,我有一个带有按钮的headerTitle
,当单击时,我想打开抽屉式导航器的抽屉菜单。现在,单击按钮只会显示错误Sub.props.navigation
不存在。
当单击标题标题按钮时,如何使抽屉菜单打开?
答案 0 :(得分:1)
请为此修改您的navigationOptions
代码:
...
static navigationOptions = ({navigation}) => ({
headerTitle: () => (
<Button
onPress={() => navigation.openDrawer()}
title="Open Drawer"
/>
)
});
...