我正在try 将数据保存在react-native中的AsyncStorage中.我想用asyncawait关键字异步保存它.

  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);
    }
  };

我在保存程序时出错

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 {

推荐答案

异步命名的arrow函数应该声明为

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);
    }
  };

React-native相关问答推荐

Appcenter构建失败错误号::EEXIST -文件存在于syserr_fail2_in

React Native 在每个页面加载时调用数据库

为什么我的expoav视频不会自动播放?

React Native:无法将具有 resizeMode包含的图像定位在父组件的 flex-end

如何在堆栈的任何屏幕的后退按钮按下时始终导航到堆栈的初始路径?

如何在 react-native 中设置 Onpress On Textinput

react-native (expo)加载markdown 文件

在根项目ReactNativeStarter中找不到任务installRelease

如何在react-native 中加密和解密文本?

如何在 React Native 中模拟align-items:baseline?

无法解析模块 react/lib/ReactUpdates

将图标添加到drawer抽屉react-navigation v5

React Native:如何获取文件大小、mime 类型和扩展名?

React-native Xcode 构建失败 -> 'RCTAssert.h file not found'

是否可以将 Image.getSize 与静态图像文件一起使用?

如何将本地文本文件加载到 React Native Project 中的字符串变量中?

使用 expo SDK react-native ios 应用程序的 Info.plist 文件

安卓启动问题:Unsupported class file major version 57

类型 androidx.appcompat.resources.R$dimen 被定义了多次

React Navigation获取堆栈标题高度