- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在所有屏幕中全局显示 HeaderLeft Menu 图标。当我点击菜单图标时,我需要显示抽屉菜单。我使用“OpenDrawer”、“CloseDrawer”方法打开/关闭抽屉菜单。
但我总是得到“undefined is not a function (evaluating 'props.navigation.openDrawer()')"
。我也尝试了以下
props.navigation.dispatch(DrawerActions.openDrawer())
props.navigation.navigate(openDrawer())
const MenuButton = (props) => {
return (
<View>
<TouchableOpacity onPress={() => { props.navigation.dispatch(DrawerActions.openDrawer())} }>
<Text>Menu</Text>
</TouchableOpacity>
</View>
)
};
const MyDrawerNavigator = createDrawerNavigator(
{
Wishist: {
screen: wishlist
},
},
{
contentComponent: SideMenuScreen,
drawerWidth: 200,
}
);
const AppNavigator = createBottomTabNavigator({
Home: {
screen: createStackNavigator({
Home: {
screen: Home
},
Contact: {
screen: Contact
}
},
{
defaultNavigationOptions: ({ navigation }) => ({
headerStyle: {
backgroundColor: 'white',
borderWidth:0,
borderBottomWidth:0
},
headerTitle: headerTitleNavigationOptions('HOME'),
headerLeft: <MenuButton navigation={navigation}/>,
headerRight: headerRightNavigatorOptions(navigation)
})
}),
navigationOptions: ({ navigation }) => ({
headerStyle: {
backgroundColor: 'white',
borderWidth:0,
borderBottomWidth:0
},
}),
}},
{
tabBarOptions: {
showLabel: false,
style: {
backgroundColor: 'white',
borderTopWidth:1
}
},
initialRouteName: 'Home',
tabBarComponent: TabBarBottom,
tabBarPosition: 'bottom',
animationEnabled: false,
swipeEnabled: false
}
);
const App = createAppContainer(AppNavigator);
最佳答案
您需要导入 DrawerActions
在您的组件中来自 react-navigation-drawer
as explained in the docs
DrawerActions is an object containing methods for generating actions specific to drawer-based navigators. Its methods expand upon the actions available in NavigationActions.
The following actions are supported:
openDrawer
- 打开抽屉closeDrawer
- 关闭抽屉 toggleDrawer
- 切换状态,即。从关闭切换到打开和import { DrawerActions } from 'react-navigation-drawer';
this.props.navigation.dispatch(DrawerActions.openDrawer())
react-navigation
api 不提供更多信息,但您可以咨询
the NavigationActions
api reference想要查询更多的信息。
All
NavigationActions
return an object that can be sent to the router usingnavigation.dispatch()
method.Note that if you want to dispatch
react-navigation
actions you should use the action creators provided in this library.
NavigationActions
在您的组件中,然后您可以
dispatch
您的操作类似于
this.props.navigation.dispatch(navigateAction);
import { NavigationActions } from 'react-navigation';
const navigateAction = NavigationActions.navigate({
routeName: 'Profile',
params: {},
action: NavigationActions.navigate({ routeName: 'SubProfileRoute' }),
});
this.props.navigation.dispatch(navigateAction);
也如
Ashwin Mothilal 中所述,请确保您通过了
navigation
组件内的 Prop 。例如,您可以运行
console.warn(props)
并立即在模拟器上看到结果。
import { DrawerActions } from 'react-navigation-drawer';
const MenuButton = (props) => {
return (
<View>
<TouchableOpacity onPress={() => {
console.warn(props);
props.navigation.dispatch(DrawerActions.openDrawer());
}}>
<Text>Menu</Text>
</TouchableOpacity>
</View>
)
};
关于react-native - react 导航 - TabNavigator-> StackNavigator 中标题菜单图标的 DrawerNavigator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56056807/
我是一名优秀的程序员,十分优秀!