使用 AsyncStorage 错误保存和检索

Saving and retrieving with AsyncStorage error

我有一个存储对象如下:

import AsyncStorage from "react-native";

const deviceStorage = {
    async saveItem(key, value) {
        try {
          await AsyncStorage.setItem(key, value);
        } catch (error) {
          console.log('AsyncStorage Error: ' + error.message);
        }
      }
};

export default deviceStorage;

setItem 抛出错误

Property 'setItem' does not exist on type 'typeof import("/Users/Developer/React/reactRallyApp/node_modules/@types/react-native/index")'

我不明白为什么会这样或如何解决,我是不是漏掉了一些简单的东西?

  1. AsyncStorage 将从 React Native 核心中删除,请改用它:https://github.com/react-native-community/react-native-async-storage
  2. 您正在将函数放入没有键的对象中。

你可以这样做:

import { AsyncStorage } from "react-native";

const saveItem = async (key, value) => {
    try {
      await AsyncStorage.setItem(key, value);
    } catch (error) {
      console.log('AsyncStorage Error: ' + error.message);
    }
  }
};

export default saveItem;