André Abboud
André Abboud

Reputation: 2030

need to return value from asyncstorage

Hello developers i am using AsyncStorage in react native i am using it this way:

const AppLanguage = createStackNavigator({
    ChooseLanguage: {
        screen: ChooseLanguage,
    },
    AppIntroScreen: {
        screen: AppIntroScreen,
    },
}, {
        headerMode: 'none',
        cardStyle: {
            backgroundColor: "#ffffff",
        },
        navigationOptions: {
            headerVisible: false,
        }
    });

retrieve = () => AsyncStorage.getItem('first').then(value => {
    return value;
});

console.log(retrieve()) //return promise but need to extract value from asyncstorage

AppRegistry.registerComponent(appName, () =>
    retrieve() == 'true' ? AppIntroScreen:AppLanguage //problem here value not extracted from asyncstorage
)

here i am using the retreive method to get value of the key='first' so if the key is equal to 'true' it is opening first the AppIntroScreen else it is opening first the ChooseLanguage screen so please help!

Upvotes: 1

Views: 217

Answers (2)

Walter Shub
Walter Shub

Reputation: 662

you can't use app AppRegistry like that it doesn't recognize the screens or navigation. Make a new screen called entry in the asynccomponent did mount run your await retrieve function and navigate to the right screen based on that.

Upvotes: 1

QuokMoon
QuokMoon

Reputation: 4425

You can use await to handle async operation.

var value = await AsyncStorage.getItem('first');
console.log(value);

Your solution also seem valid, execute your code at then statement.

Upvotes: 3

Related Questions