在我的反应本机应用程序中,我使用抽屉导航器创建了一个侧菜单,当我通过滑动打开它时,该菜单运行良好。但我想做的是通过单击按钮打开它。目前我正在尝试做槽式导航道具,相关代码如下:
import { withNavigation } from 'react-navigation';
class HallsList extends React.Component {
constructor(props){
super(props);
this.state = {
isSideMenuOpen: false
};
}
renderTopView = () => {
return(
<View>
<View style = {Styles.sideMenuButton}>
<Button
onPress = {()=> {
if (this.state.isSideMenuOpen) {
{this.props.navigation.navigate('DrawerOpen')}
}
else {
{this.props.navigation.navigate('DrawerClose')}
}
this.setState({isSideMenuOpen: !this.state.isSideMenuOpen})
}
}
title = {'Side Menu'}
/>
</View> .....
export default withNavigation(HallsList);
但是当我点击侧面菜单按钮时,它会被点击,但之后什么也没有发生。
只需更改以下代码部分即可
而不是
this.props.navigation.navigate('DrawerOpen')
放
this.props.navigation.openDrawer();
而不是
this.props.navigation.navigate('DrawerClose')
放
this.props.navigation.closeDrawer();
您可以简单地使用:
this.props.navigation.toggleDrawer();
代替:
// to keep track of state of drawer
if (this.state.isSideMenuOpen) {
{this.props.navigation.navigate('DrawerOpen')}
}
else {
{this.props.navigation.navigate('DrawerClose')}
}
this.setState({isSideMenuOpen: !this.state.isSideMenuOpen})
这适用于打开关闭和切换抽屉
navigation.dispatch(DrawerActions.openDrawer());
navigation.dispatch(DrawerActions.closeDrawer()); 导航.dispatch(DrawerActions.toggleDrawer());