美文网首页
reactnative 使用AsyncStorage

reactnative 使用AsyncStorage

作者: 米开朗骑騾 | 来源:发表于2019-02-20 20:16 被阅读0次

取:

DeviceStorage.get(this.props.loginData.userId + 'home_club')
            .then(res => {
                if (res) {
                    this.setState({
                        dataSource: res
                    })
                }
            }).catch(err => {})

存:

DeviceStorage.save(this.props.loginData.userId + 'home_club', clubQueryMineState.result);

DeviceStorage类

import React, {
    AsyncStorage,
}from 'react-native';

class DeviceStorage {
    /**
     * 获取
     * @param key
     * @returns {Promise<T>|*|Promise.<TResult>}
     */

    static get(key) {
        return AsyncStorage.getItem(key).then((value) => {
            const jsonValue = JSON.parse(value);
            return jsonValue;
        });
    }

    /**
     * 保存
     * @param key
     * @param value
     * @returns {*}
     */
    static save(key, value) {
        return AsyncStorage.setItem(key, JSON.stringify(value));
    }

    /**
     * 更新
     * @param key
     * @param value
     * @returns {Promise<T>|Promise.<TResult>}
     */
    static update(key, value) {
        return DeviceStorage.get(key).then((item) => {
            value = typeof value === 'string' ? value : Object.assign({}, item, value);
            return AsyncStorage.setItem(key, JSON.stringify(value));
        });
    }

    /**
     * 删除
     * @param key
     * @returns {*}
     */
    static delete(key) {
        return AsyncStorage.removeItem(key);
    }
}

export default DeviceStorage;

相关文章

网友评论

      本文标题:reactnative 使用AsyncStorage

      本文链接:https://www.haomeiwen.com/subject/kbdkyqtx.html