简体   繁体   English

从Promise回调中导航

[英]Navigate from within Promise callback

I am just starting out with React Native and am running into the following problem when trying to navigate from within a Promise callback. 我刚开始使用React Native并尝试从Promise回调中导航时遇到以下问题。

Here is the following code I am trying to run. 这是我尝试运行的以下代码。 I want to navigate to another screen if the http request returns that the user's login information is correct. 如果http请求返回用户的登录信息正确,我想导航到另一个屏幕。

login() {
  axios({
      method: 'post',
      url: `${Globals.WebAPI}/api/authentication/login`,
      data: {
        username: this.state.username,
        password: this.state.password
      }
  })
  .then(function(response) { 
      console.log(response.data.token)
      AsyncStorage.setItem("AuthToken", response.data.token);
      this.props.navigation.navigate('PictureDetails', {base64: photo.base64});
  })
  .catch(function(error) {
      console.log(error);
  });
}



render() {
return (
  <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
    <Text>Home Screen</Text>
    <Button
      title="Go to Details"
      onPress={this.goToDetails}
    />
    <Button
      title="Signup"
      onPress={this.goToSignup}
    />
    <Text>Username</Text>
    <TextInput
    style={{height: 40, width: 200, borderColor: 'gray', borderWidth: 1}}
    onChangeText={(text) => this.setState({username: text})}
    value={this.state.username}
  />
  <Text>Password</Text>
  <TextInput
    style={{height: 40, width: 200, borderColor: 'gray', borderWidth: 1}}
    onChangeText={(text) => this.setState({password: text})}
    value={this.state.password}
  />
  <Button
      title="Login"
      onPress={this.login}
    />
  </View>
)};

This is the error that I am getting from this function: 这是我从此函数得到的错误:

undefined is not an object (evaluating 'this.props.navigation')

I feel like this error is coming from me not fully understanding the props usage, so I am hoping that this answer helps solidify my knowledge of what's going on in React Native. 我觉得这个错误是由于我没有完全理解道具的用法而引起的,所以我希望这个答案有助于巩固我对React Native的了解。

Thanks in advance 提前致谢

This part loses this binding: 这部分失去this绑定:

  .then(function(response) { 
      console.log(response.data.token)
      AsyncStorage.setItem("AuthToken", response.data.token);
      this.props.navigation.navigate('PictureDetails', {base64: photo.base64});
  })

so when you call this.props.navigation.navigate( , this is undefined , which is what your error tells you. 因此,当您调用this.props.navigation.navigate(thisundefined ,这就是您的错误告诉您的。

The easiest fix is to convert regular function to an arrow function, which lexically binds the this to surrounding context's this value. 最简单的解决方法是将常规函数转换为箭头函数,该函数将this绑定到周围上下文的this值。

  .then((response) => { 
      console.log(response.data.token)
      AsyncStorage.setItem("AuthToken", response.data.token);
      this.props.navigation.navigate('PictureDetails', {base64: photo.base64});
  })

More info here 更多信息在这里

I guess you didn't create a Navigation object using react-navigation library. 我猜您没有使用react-navigation库创建Navigation对象。 I need to see the whole file of this code to make sure what the reason is. 我需要查看此代码的整个文件,以确保原因是什么。

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

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