0

我想在另一个函数中使用存储在 AsyncStorage 中的值。

这是我的 ValueHandler.js

import { AsyncStorage } from 'react-native';

export async function getSavedValue() {
  try {
    const val = await AsyncStorage.getItem('@MyStore:savedValue');
    console.log("#getSavedValue", val);
    return val;
  } catch (error) {
    console.log("#getSavedValue", error);
  }
  return null;
}

我做了上面的函数 getSavedValue() 来获取存储在 AsyncStorage 中的 savedValue 的值。

import {getSavedValue} from '../lib/ValueHandler';
import Api from '../lib/Api';

export function fetchData() {
  return(dispatch, getState) => {
    var params = [
      'client_id=1234567890',
    ];
    console.log("#getSavedValue()", getSavedValue());
    if(getSavedValue() != null) {
      params = [
        ...params,
        'savedValue='+getSavedValue()
      ];
    }
    params = params.join('&');
    console.log(params);
    return Api.get(`/posts/5132?${params}`).then(resp => {
      console.log(resp.posts);
      dispatch(setFetchedPosts({ post: resp.posts }));
    }).catch( (ex) => {
      console.log(ex);
    });
  }
}

我如何实现这一目标?我真的想要一个简单的方法来在本地存储中保存一个值,并在我需要进行 API 调用时简单地检索它。

有人对此有任何建议或解决方案吗?

4

2 回答 2

0

尝试这个:

添加async到,return (dispatch, getState) => {以便您可以在其中等待

import {getSavedValue} from '../lib/ValueHandler';
import Api from '../lib/Api';

export function fetchData() {
    return async (dispatch, getState) => {
        let params = [
            'client_id=1234567890',
        ];
        const val = await getSavedValue();
        if (val != null) {
            params = [...params, 'savedValue='+val];
    // why not 
    //      params.push(`savedValue=${val}`);
    //
        }
        params = params.join('&');
        return Api.get(`/posts/5132?${params}`))
        .then(resp => {
                dispatch(setFetchedPosts({ post: resp.posts }));
        })
        .catch( (ex) => {
            console.log(ex);
        });
    };
}
于 2017-11-02T11:12:45.260 回答
0

你可以这样做:

import { AsyncStorage } from 'react-native';

export async function getSavedValue() {
    return new Promise ((resolve, reject) => { 
      try {
        const val = await AsyncStorage.getItem('@MyStore:savedValue');
        console.log("#getSavedValue", val);
        resolve(val);
      }catch (error) {
        console.log("#getSavedValue", error);
        reject(error)
      }  
   })
}


import {getSavedValue} from '../lib/ValueHandler';
import Api from '../lib/Api';

export function fetchData() {
  return async (dispatch, getState) => {
    var params = [
      'client_id=1234567890',
    ];
    console.log("#getSavedValue()", getSavedValue());
    if(getSavedValue() != null) {
      params = [
        ...params,
        'savedValue='+ await getSavedValue()
      ];
    }
    params = params.join('&');
    console.log(params);
    return Api.get(`/posts/5132?${params}`).then(resp => {
      console.log(resp.posts);
      dispatch(setFetchedPosts({ post: resp.posts }));
    }).catch( (ex) => {
      console.log(ex);
    });
  }
}
于 2017-11-01T17:28:23.257 回答