第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

【學(xué)習(xí)打卡】第7天 React Native 本地存儲(chǔ)

標(biāo)簽:
React Native

课程名称:RN入门到进阶,打造高质量上线App(2022全新升级)

课程章节:3-5 .React Native数据库编程之AsyncStorage精讲

主讲老师:CrazyCodeBoy

课程内容:今天学习的主要内容包括:AsyncStorage的使用和封装

课程收获:

  1. React Native Async Storage 的安装

使用yarn来进行安装

执行命令

yarn add @react-native-async-storage/async-storage

在Android上不需要额外的操作,因为项目是大于0.60的版本,

在ios上,需要使用CocoaPods 将原生添加到项目中:RNAsyncStorage

npx pod-install
  1. 用法

Async Storage 是用来进行数据存储的,类型于浏览器的LocalStorage是一个持久化存储的方案。

在使用Async Storage的时候,我们需要对数据进行序列化操作

先导入方法

import AsyncStorage from '@react-native-async-storage/async-storage';

存储数据

setItem是用来进行数据存储的。

存储字符串

const storeData = async (value) => {
  try {
    await AsyncStorage.setItem('@storage_Key', value)
  } catch (e) {
    // saving error
  }
}

存储对象

const storeData = async (value) => {
  try {
    const jsonValue = JSON.stringify(value)
    await AsyncStorage.setItem('@storage_Key', jsonValue)
  } catch (e) {
    // saving error
  }
}

获取数据

getItem是进行数据读取的。如果没有找到存的数据,将会返回一个null

获取字符串数据

const getData = async () => {
  try {
    const value = await AsyncStorage.getItem('@storage_Key')
    if(value !== null) {
      // value previously stored
    }
  } catch(e) {
    // error reading value
  }
}

获取对象

const getData = async () => {
  try {
    const jsonValue = await AsyncStorage.getItem('@storage_Key')
    return jsonValue != null ? JSON.parse(jsonValue) : null;
  } catch(e) {
    // error reading value
  }
}
  1. 对存储方法进行封装。

对于数据的存储,使用上述的代码都过于麻烦,我们可以把这些方法封装起来,然后进行调用

代码:

import AsyncStorage from '@react-native-async-storage/async-storage';

export default class StorageUtil {
  /**
   * 获取数据
   * @param key 数据的key值
   * @return {Promise<any> | Promise}
   */
  static getItem(key: string) {
    return new Promise<any>((resolve, reject) => {
      AsyncStorage.getItem(key, (error, result) => {
        if (error) {
          reject(error);
          return;
        }
        // 判断是否是个对象
        try {
          resolve(JSON.parse(result!));
        } catch (e) {
          resolve(result);
        }
      });
    });
  }

  /**
   * 保存数据
   * @param key
   * @param data
   */
  static setItem(key: string, data: any) {
    data = typeof data === 'object' ? JSON.stringify(data) : data;
    AsyncStorage.setItem(key, data, error => {
      if (error) {
        //Tips.toast('保存失败');
      }
    });
  }

  /**
   * 删除数据
   * @param key
   */
  static removeItem(key: string) {
    AsyncStorage.removeItem(key, error => {
      if (error) {
        //Tips.toast('删除失败');
      }
    });
  }

  /**
   *  删除json文件
   */
  static clear() {
    AsyncStorage.clear(error => {
      if (error) {
        //Tips.toast('文件删除失败');
      }
    });
  }
}

今天学习课程加练习一共用了30分钟,主要是学习对React Native本地存储的使用。

图片描述

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消