Arrow功能与async和await in react native [英] Arrow functions with async and await in react native

查看:127
本文介绍了Arrow功能与async和await in react native的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 react-native 中的 AsyncStorage 中保存数据。我想保存它是异步的,所以使用 async 等待关键字。

I am trying to save data in AsyncStorage in react-native. I want to save it asynchronous so using async and await keyword.

  async onPositiveClickListener = () => {
    // user has completed product tour_end
    try {
      await AsyncStorage.setItem("@ProductTour:key", "true");
      const { navigate } = this.props.navigation;
      navigate("DashboardScreen");
    } catch (error) {
      console.log(error);
    }
  };

我在保存程序时遇到错误

I am getting an error while saving program

SyntaxError: Unexpected token, expected ( (40:32)
  38 |   };
  39 | 
> 40 |   async onPositiveClickListener = () => {
     |                                 ^
  41 |     // save user has completed product tour_end
  42 |     try {
  43 |       await AsyncStorage.setItem("@ProductTour:key", "true");
Hide Stack Trace
SyntaxError: Unexpected token, expected ( (40:32)
  38 |   };
  39 | 
> 40 |   async onPositiveClickListener = () => {
     |                                 ^
  41 |     // save user has completed product tour_end
  42 |     try {


推荐答案

异步命名箭头函数应声明为

Async named arrow function should be declared like

const onPositiveClickListener = async () => {
    // user has completed product tour_end
    try {
      await AsyncStorage.setItem("@ProductTour:key", "true");
      const { navigate } = this.props.navigation;
      navigate("DashboardScreen");
    } catch (error) {
      console.log(error);
    }
  };

这篇关于Arrow功能与async和await in react native的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆