React native 抽屉菜单未关闭
我使用react导航的抽屉激活功能创建了一个抽屉菜单。我想创建一个按钮来关闭菜单,但onPress功能似乎不起作用 我从“react navigation drawer”中导入了Drawracts,并尝试使用不同的语法 例如)this.props.navigation.dispatch(DrawerActions.closeDrawer()); this.props.navigation.closeDrawer()React native 抽屉菜单未关闭,react-native,react-navigation,React Native,React Navigation,我使用react导航的抽屉激活功能创建了一个抽屉菜单。我想创建一个按钮来关闭菜单,但onPress功能似乎不起作用 我从“react navigation drawer”中导入了Drawracts,并尝试使用不同的语法 例如)this.props.navigation.dispatch(DrawerActions.closeDrawer()); this.props.navigation.closeDrawer() import React,{Component}来自'React'; 导入{ 形
import React,{Component}来自'React';
导入{
形象,,
样式表,
文本,
图像背景,
可触摸且无反馈,
看法
按钮
滚动视图,
}从“反应本机”;
导入{
createDrawerNavigator,
createStackNavigator,
createAppContainer,
付款人,
安全区域视图,
航行
}从“反应导航”;
从“反应导航抽屉”导入{DrawerActions,};
const navigator=createDrawerNavigator(
{
家:兰德,
第1页:着陆器,
第2页:着陆器,
第3页:着陆器,
第4页:着陆器,
},
{
contentComponent:(道具)=>(
this.props.navigation.dispatch(DrawerActions.closeDrawer())}>
)
},
);
我最终希望能够点击x按钮,让它将我重定向到主屏幕
我得到以下错误
Undefined不是对象(正在计算“\u this.props.navigation”)
您正在从错误的包导入
DrawerActions
改变
import { DrawerActions, } from 'react-navigation-drawer';
到
要关闭它,你要像
onPress={() => this.props.navigation.dispatch(DrawerActions.openDrawer())}
编辑:
你做错了的是,你得到了这个.props
,但它只是props
contentComponent: (props) => (
<SafeAreaView>
<View style= {{backgroundColor:'black'}}>
{// changed to props without this}
<TouchableWithoutFeedback onPress={() => props.navigation.dispatch(DrawerActions.closeDrawer())}>
<Image source={require('./Images/x.png')} style = {styles.cross}/>
</TouchableWithoutFeedback>
</View>
<ScrollView style= {{backgroundColor: 'black', paddingLeft: '5%'}}>
<DrawerItems {...props} />
</ScrollView>
</SafeAreaView>
)
contentComponent:(道具)=>(
{//更改为没有此属性的道具}
props.navigation.dispatch(DrawerActions.closeDrawer())}>
)
您正在从错误的软件包导入抽屉操作
改变
import { DrawerActions, } from 'react-navigation-drawer';
到
要关闭它,你要像
onPress={() => this.props.navigation.dispatch(DrawerActions.openDrawer())}
编辑:
你做错了的是,你得到了这个.props
,但它只是props
contentComponent: (props) => (
<SafeAreaView>
<View style= {{backgroundColor:'black'}}>
{// changed to props without this}
<TouchableWithoutFeedback onPress={() => props.navigation.dispatch(DrawerActions.closeDrawer())}>
<Image source={require('./Images/x.png')} style = {styles.cross}/>
</TouchableWithoutFeedback>
</View>
<ScrollView style= {{backgroundColor: 'black', paddingLeft: '5%'}}>
<DrawerItems {...props} />
</ScrollView>
</SafeAreaView>
)
contentComponent:(道具)=>(
{//更改为没有此属性的道具}
props.navigation.dispatch(DrawerActions.closeDrawer())}>
)
希望就是帮助
hope is helps尝试检查此项:
this.props.navigation.closeDrawer()
出现相同错误:“未定义的不是对象(计算“\u this.props.navigation”)”尝试检查此项:this.props.navigation.closeDrawer()
出现相同错误:“未定义的不是对象(计算“\u this.props.navigation”)”您正在使用this.props.navigation
但它应该仅为props.navigation
您正在使用this.props.navigation
但它应该仅为props.navigation