Home > Enterprise >  Why my app is only showing loading screen? and warn me TypeError: undefined is not an object (evalua
Why my app is only showing loading screen? and warn me TypeError: undefined is not an object (evalua

Time:11-18

Why my app is only showing loading screen? when i am running my app than its only loading and not showing other screens and showing a WARN that

` WARN Possible Unhandled Promise Rejection (id: 0): TypeError: undefined is

i think it has problem with this code

import React, { useEffect } from 'react';
import { View, ActivityIndicator, StyleSheet} from 'react-native';
import  { AsyncStorage } from '@react-native-async-storage/async-storage';

import { useDispatch } from 'react-redux';

import Colors from '../constants/Colors';
import * as authActions from '../store/actions/auth';

const StartupScreen = props => {

    const dispatch = useDispatch();

    useEffect(() => {
        const tryLogin = async () => {
            const userData = await AsyncStorage.getItem('userData');
            if (!userData) {
                // props.navigation.navigate('Auth');
                dispatch(authActions.setDidTryAutoLogin());
                return;
            }
            const transformedData = JSON.parse(userData);
            const { token, user } = transformedData;

            // props.navigation.navigate('Shop');
            dispatch(authActions.authenticate(user, token));
        };

        tryLogin();
}, [dispatch])


return (
    <View style={styles.screen}>
        <ActivityIndicator size="large" color={Colors.primary} />
    </View>
);
};


const styles = StyleSheet.create({
    screen: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center'
    }
});

export default StartupScreen;

CodePudding user response:

According to the docs you import is wrong.

This:

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

should be

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

Basically, what you are trying to do with {AsyncStorage} is you are importing "part" of the exported code called AsyncStorage. That is why the error says: _asyncStorage.AsyncStorage.getItem') (asyncStorage mentioned twice) and you need the entire object out of the package.

  • Related