0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

react native 0.73で内部ストレージを扱う

Posted at

AsyncStorageがいつの間にか使えなくなってしまっていた。

公式はここで探せとアナウンスしているので
目についたreact-native-mmkv-storageを試してみました。

こんな感じでコードを書いてみると

import { MMKVInstance, MMKVLoader } from "react-native-mmkv-storage";

export default class Internal {
    static initialized = false
    static storage: MMKVInstance | undefined

    static init() {
        this.storage = new MMKVLoader().withInstanceID('settings').initialize()
        this.initialized = true
    }

    static load(key: string): string {
        if (!this.initialized){
            this.init()
        }
        const result =  this.storage?.getString(key) ?? ""
        console.log("loaded:" + result)
        return result
    }

    static save(key: string, value: any) {
        if (!this.initialized){
            this.init()
        }
        this.storage?.setString(key, "" + value);
    }
}

保存と読み込みが出来ました
mmkvstorage.png

ちなみにiOSしか試してません。
AndroidはiOSが動けば多分動くでしょう。
permissionさえ与えてやればiOSよりはゆるいはずなので

また、公式にもある通り、イベントトリガでの読み書きも出来るようです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?