Return Asyncstorage value in a constant

Asked

Viewed 580 times

0

I’ve been trying for some time to return an item stored in the Asyncstorage of React Active, but the return is always a Promise, but what I want is to make decisions with this return later, and be able to access the value outside the scope of Promise! what I want to do is:

const user = await AsyncStorage.getItem('user')

So you can use the constant user Is it possible afterwards? If it is not possible it is really necessary to encompass all that I wish to do within the Premomise? and what is the alternative for this?

EDIT

The goal of using Asyncstorage here is to recover the logged in user, so I can direct it to home if it is logged in or to login if it is not on the App startup. The idea is to change the initialRouteName. Something like this:

let initialScreen = 'Login'
AsyncStorage.getItem('user').then(user => {
  if(user) initialScreen = 'Home'
})

const AppNavigator = createStackNavigator({
  Home: {
    screen: Home,
      navigationOptions: {
      title: 'Home',
      header: null
    },
  },
  Login: {
    screen: Login,
    navigationOptions: {
      title: 'Login',
      header: null
    },
  }
}, {initialRouteName: initialScreen});

const AppContainer = createAppContainer(AppNavigator)
export default AppContainer

Thank you very much!

  • the query return is a json string or just a simple string?

4 answers

1


use a function async and put await prior to Asyncstorage:

async function pegarValor(){
    const myuser = await AsyncStorage.getItem('user')
    console.log(myuser)
}


pegarValor();

1

const AppNavigator = AsyncStorage.getItem('user').then(user => {
    if (user) {
        initialScreen = 'Home'
        this.user = user

        return createStackNavigator({
            Home: {
                screen: Home,
                navigationOptions: {
                    title: 'Home',
                    header: null
                },
            },
            Login: {
                screen: Login,
                navigationOptions: {
                    title: 'Login',
                    header: null
                },
            }
        }, { initialRouteName: 'Login' });
    }
})

const AppContainer = createAppContainer(AppNavigator)
export default AppContainer
  • I tried this way and got the following error: Undefined is not an Object (evaluating 'Component.router.getStateForAction'), I’m almost using a modal for that menu! haha

  • 1

    Bro, I changed the code, see what it gives now

  • Unfortunately it did not roll, I get the same error (evaluating 'Component.router.getStateForAction')

0

It’s okay to do this. Example:

async function Test() {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve({"testando":123});
        }, 1000);
    });
}

const test = await Test();
console.log(test);
  • Unfortunately it does not work this way, I get error in await in build

0

Or you can put it in the component DidMount:

import React, { Component } from 'react';
import { AsyncStorage } from 'react-native';
...
let user;
class sua_class extends Component {
  async componentDidMount {
    user = await AsyncStorage.getItem('user');
  }
render(){...}
}
  • Voce is not using Redux?

  • If he uses Redux from the right tbm. Anyway it is a Promise then you need to wait for the answer.

  • Interesting, but I could not apply in my case... is there another alternative? Localstorage for example?

  • 1

    You’re using React Native right? It’s only with Asyncstorage. It’s asynchronous and it uses async/await. Put it like you’re doing and what mistake you’re making to help better.

  • I edited above, and added the code I am trying to implement, in my case it is not a class Component as I could not find any way to use the createAppContainer with class Components

Browser other questions tagged

You are not signed in. Login or sign up in order to post.