React Native 为什么我的应用程序只显示加载屏幕?并警告我TypeError:undefined不是对象(评估'_asyncStorage.AsyncStorage. getItem')

rxztt3cl  于 2022-11-25  发布在  React
关注(0)|答案(1)|浏览(147)

为什么我的应用程序只显示加载屏幕?当我运行我的应用程序比它只加载,不显示其他屏幕,并显示警告
'WARN可能未处理的承诺拒绝(id:0):类型错误:未定义为
我认为这段代码有问题

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;
a5g8bdjr

a5g8bdjr1#

您导入的单据显示有误。
这一点:

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

应该是

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

基本上,您尝试对{AsyncStorage}执行的操作是导入名为AsyncStorage的导出代码的“一部分”。这就是为什么错误显示为:_asyncStorage.AsyncStorage.getItem')asyncStorage被提到了两次),并且您需要将整个对象从包中取出。

相关问题