导航在功能中不起作用

时间:2018-05-06 14:27:58

标签: react-native react-navigation react-native-fbsdk

我有一个使用react-native-fbsdk登录facebook的功能:

ListaUf

当用户成功登录后,我想导航到下一页,名为this.props.navigation.navigate('ListaUf')

如果我在按钮或componentDidMount中使用alert('Login ok'),则此操作正常。

在模拟器上,出现黄色消息:

  

TypeError:undefined不是一个函数(评估' this.props.navigation')

this.props.navigation.navigate('ListaUf')有效,但{{1}}不起作用。

2 个答案:

答案 0 :(得分:1)

你正在失去this的背景。您需要bind您的功能或使用arrow functions。如果按下按钮或类似按钮触发handleFacebookLogin,您也需要bind

使用bind

handleFacebookLogin () {
  LoginManager.logInWithReadPermissions(['public_profile']).then(
    function(result) {
      if (result.isCancelled) {
        alert('Login isCancelled');
      } else {
        alert('Login ok')
        this.props.navigation.navigate('ListaUf')
      }
    }.bind(this),
    function(error) {
      alert('Error: ' + error);
    }
  );
}

使用lambda

handleFacebookLogin = () => {
  LoginManager.logInWithReadPermissions(['public_profile']).then(
    (result) => {
      if (result.isCancelled) {
        alert('Login isCancelled');
      } else {
        alert('Login ok')
        this.props.navigation.navigate('ListaUf')
      }
    },
    function(error) {
      alert('Error: ' + error);
    }
  );
}

答案 1 :(得分:0)

您是否尝试在导航周围添加花括号

导出默认值 ({navigation}) =>