简体   繁体   English

navigation()抛出错误-反应本机

[英]navigate() throwing error - react native

I am trying to sign out my firebase user, and in the onPress of the icon I am using to sign out I have a function that executes a command to navigate back to the login screen. 我正在尝试注销我的firebase用户,并且在我用来注销的图标的onPress中,我具有一个函数,该函数执行命令以导航回登录屏幕。 The logout with firebase.auth.signOut is successful, I just get this red screen: 使用firebase.auth.signOut登出成功,我得到的是红色屏幕:

在此处输入图片说明

Here is my relevant code: 这是我的相关代码:

static navigationOptions = {
    headerTitle: 'Inventory Control',
    headerLeft: null,
    headerRight: (
      <Icon name={'exit-to-app'}
            onPress={() => { firebase.auth().signOut()
                              .then(() => { 
                                  this.leave();
                              })
                              .catch(err => {
                                Alert.alert(err);
                              })
                           } 
                    }
      />
    ),
    headerTitleStyle: {textAlign: 'center', flex: 1, backgroundColor: '#ddeaff', color: '#518dff', fontFamily: 'American Typewriter', fontWeight: 'bold', fontSize: 20},
    headerStyle: {backgroundColor: '#ddeaff'}
};

leave = () => {
    const { navigate } = this.props.navigation;
    navigate('LogIn').catch(err => {Alert.alert(err)});
}

No errors are caught, just the red screen. 没有错误被捕获,只有红色屏幕。 As far as I can tell the error is coming from the navigate call - the actual navigation to the LogIn page never happens, it just stays on the screen that I logged out on. 据我所知错误是来自navigate调用的-到LogIn页面的实际导航从未发生,它只是停留在我注销的屏幕上。 Any help is appreciated, thanks. 任何帮助表示赞赏,谢谢。

UPDATE UPDATE

I think it has something to do with using this inside the onPress inside the navigationOptions - possibly because this is referring to a scope outside of the component that it is in? 我认为这与在navigationOptionsonPress内部使用this有关-可能是因为this是指它所位于的组件外部的作用域? How do I handle using this ? this如何使用呢? When I comment out const { navigate } ... the error goes away, so it has something to do with this.props.navigation I believe (and this ?). 当我注释掉const { navigate } ... this.props.navigation const { navigate } ... ,错误消失了,所以它与我相信的this.props.navigation (并且与this ?)。

UPDATE UPDATE

I think I am correct about the this keyword, the below code produces an error: 我认为我对this关键字是正确的,以下代码会产生错误:

....

headerRight: (
      <Icon name={'exit-to-app'}
            onPress={() => {
              //firebase.auth().signOut()
                //.then(() => { 
                  const { navigate } = this.props.navigation;
                  navigate('LogIn').catch(err => {Alert.alert(err)});
                //})
                //.catch(err => {
                  //Alert.alert(err);
                //})
            }}
      />
    ),

....

The error is: undefined is not an object (evaluating '_this4.props.navigation') 错误是: undefined is not an object (evaluating '_this4.props.navigation')

There's been a recent update to react-navigation with other errors circulating, due to the recent update https://github.com/react-navigation/react-navigation/releases 由于最近的更新https://github.com/react-navigation/react-navigation/releases ,所以最近进行了更新以react-navigation与其他循环错误。

If you are using version 2.12.0 I suggest downgrading to 2.11.2 , till bugs are fixed. 如果您使用的是2.12.0版,建议将其降级到2.11.2,直到修复错误为止。

This worked: 这工作:

static navigationOptions = ({ navigation }) => {
    return {
      headerTitle: 'Inventory Control',
      headerLeft: null,
      headerRight: (
        <Icon name={'exit-to-app'}
              onPress={() => {
                firebase.auth().signOut()
                  .then(() => { 
                    //const { navigate } = this.props.navigation;
                    //Alert.alert(Object.keys(navigation));
                    navigation.navigate('LogIn');
                  })
                  .catch(err => {
                    Alert.alert(err);
                  })
              }}
        />
      ),
      headerTitleStyle: {textAlign: 'center', flex: 1, backgroundColor: '#ddeaff', color: '#518dff', fontFamily: 'American Typewriter', fontWeight: 'bold', fontSize: 20},
      headerStyle: {backgroundColor: '#ddeaff'}
    }
};

I had to use the navigation method that gets passed with navigationOptions to make the navigation. 我必须使用与navigationOptions一起传递的navigation方法来进行导航。

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

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