react native开发Android 篇——使用AsyncStorage从@react-native-community/async-storage引入

react native开发Android 篇——使用AsyncStorage从@react-native-community/async-storage引入

import { AsyncStorage } from "react-native";//这样写会报以下的错误

react native开发Android 篇——使用AsyncStorage从@react-native-community/async-storage引入_第1张图片

//安装
yarn add @react-native-community/async-storage/npm install @react-native-community/async-storage --save
//关联react-native原生代码
react-native link @react-native-community/async-storage
//使用
import AsyncStorage from '@react-native-community/async-storage';
//储存
function storage(key,value){
    try {
      AsyncStorage.setItem(
        key,
        value,
        (error) => {
          console.log(error)
        }
      );
    } catch (error) {
        console.log(error)
    }
  }
  //读取
function  readStorage(key){
    try {
      AsyncStorage.getItem(
        key,
        (error, result) => {
            console.log(error)
            console.log(result)
            return result
        }
      );
    } catch (error) {
        console.log(error)
    }
  }

你可能感兴趣的:(react-native)