简体   繁体   English

React-Native:后退按钮设置保留在所有组件上

[英]React-Native: Back button settings stays on all components

So at the moment I have a small login system.所以目前我有一个小的登录系统。 After you are logged in if you press the back button it will ask you if you want to log out and get sent back to login page, the only problem is that if you press back button on the Login page after you logged out, its asking you again if you want to log out, how I add a back button setting for each component?登录后,如果您按后退按钮,它会询问您是否要注销并返回登录页面,唯一的问题是如果您在注销后在登录页面上按后退按钮,它会询问如果你想退出,我如何为每个组件添加后退按钮设置?

(I'm using react-native-flux-router for my routes) (我正在为我的路线使用 react-native-flux-router)

componentWillMount() {
    this._loadSessionToken().done();
    BackAndroid.addEventListener('hardwareBackPress', function() {
        Alert.alert(
            'Logout',
            'Do you want to log out?',
            [
                {text: 'Yes', onPress: () => Actions.login({text: 'Logout'})},
                {text: 'No'}
            ]
        );
        return true;
    });
};

Make the function which handles the back button press in a separate module.使处理后退按钮的函数在单独的模块中按下。 If you want to remove the event listener from within logout page itself, handle it in componentWillUnmount as follows:如果要从注销页面本身中删除事件侦听器,请在 componentWillUnmount 中按如下方式处理它:

function componentWillUnmount() {
  BackAndroid.removeEventListener('hardwareBackPress', fn);
}

If you want to remove the event listener from the login page, import the fn.如果要从登录页面中删除事件侦听器,请导入 fn。 And handle it in componentWillMount.并在 componentWillMount 中处理它。

import { fn } from '../common/backpress';

function componentWillMount() {
  BackAndroid.removeEventListener('hardwareBackPress', fn);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM