【问题标题】:What is the way to use the AsyncStorage variable in my situation?在我的情况下使用 AsyncStorage 变量的方法是什么?
【发布时间】:2020-11-23 19:08:42
【问题描述】:

在以下情况下使用AsyncStorage 的正确方法是什么:

我正在尝试使用userName,它保存在存储中:

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

const userName = await AsyncStorage.getItem('AZURE-USERNAME');

我只想打印变量“userName

export const deleteFromTable = () => {

  console.log('MY ASYNC ================>>>>>>>>>>>', userName);

  fetchUserConnectResponseData().then((result) => {
    console.log('before', JSON.stringify(result));
    deleteUserConnectResponseData().then(() => {
      fetchUserConnectResponseData().then((result1) => {
        console.log('after', JSON.stringify(result1));
      });
    });
  });
};

【问题讨论】:

  • 你能在这里添加你的完整代码吗?看来您没有在正确的位置获得userName(在您的 deleteFromTable 内)
  • @shira 你能不能把这个deleteFromTable 变成一个异步函数并在里面做await 或者你可以尝试使用AsyncStorage.getItem('AZURE-USERNAME').then(val => {console.log("username:" , val)})

标签: javascript reactjs react-native async-await asyncstorage


【解决方案1】:

也许你可以通过它使用react-native-easy-app,你可以同步访问AsyncStorage中的数据,让AsyncStorage更易用。

【讨论】:

    【解决方案2】:

    Ciao,我想你将数据保存到 AsyncStorage 某处,例如:

    AsyncStorage.setItem('AZURE-USERNAME', 'user name I want to save')
    

    好吧,您可以通过这种方式在代码中检索它:

    export const deleteFromTable = () => {
    
      let userName = "";
    
      AsyncStorage.getItem('AZURE-USERNAME').then((userName) => {
         console.log('MY ASYNC ================>>>>>>>>>>>', userName);
    
         fetchUserConnectResponseData().then((result) => {
         console.log('before', JSON.stringify(result));
         deleteUserConnectResponseData().then(() => {
            fetchUserConnectResponseData().then((result1) => {
               console.log('after', JSON.stringify(result1));
            });
          });
        });
      });
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 2012-07-12
      • 2023-03-13
      相关资源
      最近更新 更多