goback()在反应导航v2中不起作用

时间:2018-05-22 08:57:24

标签: react-native

我是抽屉我使用了以下代码

主页代码:

 @Html.TextBoxFor(model => model.DatemailedStart, new {id = "MailStartDate", placeholder = "MM/DD/YYYY", maxlength = "40", @class = "TextboxDates", @onblur = "isValidDate('MailStartDate');" })

  function isValidDate(id) {
    var dateString = $('#'+id+'').val();
  }

我的主页的索引代码:

   export default class HomeScreen extends Component {
        render() {
            return (
                <Container>
                    <Header>
                        <Left/>
                        <Body>
...
                        </Body>
                        <Right style={{flex: 1}}>
                            <Button transparent onPress={() => {
                                this.props.navigation.openDrawer();
                                console.log('drawer');
                            }}>
                                <Icon name='menu'/>
                            </Button>
                        </Right>
                    </Header>
                    <Content>
                    </Content>
                </Container>
            );}
        }

我的AboutUs页面的索引代码:

export default HomeScreenRouter = createDrawerNavigator({
     Home: {screen: HomeScreen},
     AboutUs: {screen: AboutUs}, 
    },{ 
    contentComponent: props => <SideBar {...props} />, 
    drawerPosition: "right",
     initialRouteName: 'Home', });

关于我们代码:

export default (DrawNav = createStackNavigator
    ({ AboutUs: {screen: AboutUs}, 
    },));

此代码在版本1中没有问题

但是在版本2中,返回按钮不起作用

当我使用手机的后退按钮时,应用关闭

1 个答案:

答案 0 :(得分:0)

这可能会对你有帮助。

//禁用设备返回按钮的页面代码

从'react-native'导入{BackHandler};

componentWillUnmount(){

BackHandler.removeEventListener('hardwareBackPress', 
this.handleBackButton);

}

componentDidMount(){

BackHandler.addEventListener( 'hardwareBackPress',this.handleBackButton); }

handleBackButton(){

return true;

}

//设备返回按钮工作的页面代码和返回上一页的自定义后退按钮

从'react-native'导入{BackHandler};

componentWillMount(){

BackHandler.addEventListener('hardwareBackPress', 
this.handleBackButtonClick);

}

componentWillUnmount(){

BackHandler.removeEventListener('hardwareBackPress', 
this.handleBackButtonClick);

}

handleBackButtonClick(){

this.props.navigation.goBack(null);
return true;

}

按钮透明  onPress = {()=&gt; GoBack的()}