React native TypeError:undefined不是对象(正在计算';loginState.isLoading';)

React native TypeError:undefined不是对象(正在计算';loginState.isLoading';),react-native,typeerror,asyncstorage,React Native,Typeerror,Asyncstorage,我正在尝试使用带有React Native的异步存储对登录进行身份验证。每次我点击模拟器中的注册按钮,它都会抛出一个类型错误,说“TypeError:undefined不是一个对象(评估'loginState.isLoading')”。我相信问题在于如何定义loginState。下面是我定义loginState的页面: import 'react-native-gesture-handler'; import { StatusBar } from 'expo-status-bar'; impor

我正在尝试使用带有React Native的异步存储对登录进行身份验证。每次我点击模拟器中的注册按钮,它都会抛出一个类型错误,说“TypeError:undefined不是一个对象(评估'loginState.isLoading')”。我相信问题在于如何定义loginState。下面是我定义loginState的页面:

import 'react-native-gesture-handler';
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet,Component, Text,TouchableWithoutFeedback, View,Image,SafeAreaView, Button, ScrollView, ActivityIndicator } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createDrawerNavigator } from '@react-navigation/drawer';


import MainTabScreen from './screens/MainTab';
import {DrawerContent} from './screens/DrawerContent';
import RootStackScreen from './screens/RootStack'
import { useEffect } from 'react';
//import { firebaseConfig } from './screens/config';
import { AuthContext } from './components/context';
import AsyncStorage from '@react-native-async-storage/async-storage';

 

const Drawer = createDrawerNavigator();

function App() {
  // const [isLoading, setIsLoading] = React.useState(true);
  // const [userToken, setUserToken] = React.useState(null); 

  const initialLoginState = {
    isLoading: true,
    userName: null,
    userToken: null,
  };

  const loginReducer = (prevState, action) => {
    switch( action.type ) {
      case 'RETRIEVE_TOKEN': 
        return {
          ...prevState,
          userToken: action.token,
          isLoading: false,
        };
      case 'LOGIN': 
        return {
          ...prevState,
          userName: action.id,
          userToken: action.token,
          isLoading: false,
        };
      case 'LOGOUT': 
        return {
          ...prevState,
          userName: null,
          userToken: null,
          isLoading: false,
        };
      case 'REGISTER': 
        return {
          ...prevState,
          userName: action.id,
          userToken: action.token,
          isLoading: false,
        };
    }
  };

  const [loginState, dispatch] = React.useReducer(loginReducer, initialLoginState);

  const authContext = React.useMemo(() => ({
    signIn: (userName,password) =>{
      // setUserToken('tony');
      // setIsLoading(false);
      let userToken;
      userToken = null;
      if (userName == 'user' && password == 'pass'){
        userToken = 'lexi';
      }
      dispatch({type:'LOGIN,',id:userName, token:userToken});
    },
    signOut: () => {
      // setUserToken(null);
      // setIsLoading(false);
      dispatch({type:'LOGOUT'});
    },
    signUp: () => {
      setUserToken('tony');
      setIsLoading(false);
    },
  }),[])

  useEffect(() => {
    setTimeout(() => {
     // setIsLoading(false);
     dispatch({type:'RETRIEVE_TOKEN',token:'mario'});
    },1000);
  },[]);


  if (loginState.isLoading == true)
  {
    return(
      <View style = {{flex:1,justifyContent:'center',alignItems:'center'}}>
        <ActivityIndicator size = "large"/>
      </View>
    );
  }

  return (
    <AuthContext.Provider value = {authContext}>
    <NavigationContainer>
      {loginState.userToken !== null ? (
        <Drawer.Navigator drawerContent = {props => <DrawerContent {... props}/>}>
          <Drawer.Screen name="HomeDrawer" component={MainTabScreen} />
        </Drawer.Navigator> 
      )
      :
        <RootStackScreen/>
      }
    </NavigationContainer> 
    </AuthContext.Provider>
  );
}

export default App;
导入“反应本机手势处理程序”;
从“博览会状态栏”导入{StatusBar};
从“React”导入React;
从“react native”导入{样式表、组件、文本、无反馈触摸屏、视图、图像、安全区域视图、按钮、滚动视图、活动指示器};
从'@react-navigation/native'导入{NavigationContainer};
从'@react导航/drawer'导入{createDrawerNavigator};
从“/screens/MainTab”导入MainTabScreen;
从“/screens/DrawerContent”导入{DrawerContent};
从“./screens/RootStack”导入RootStackScreen
从“react”导入{useffect};
//从“/screens/config”导入{firebaseConfig};
从“./components/context”导入{AuthContext};
从“@react native async storage/async storage”导入异步存储;
const Drawer=createDrawerNavigator();
函数App(){
//const[isLoading,setIsLoading]=React.useState(true);
//const[userToken,setUserToken]=React.useState(null);
常量initialLoginState={
孤岛加载:是的,
用户名:null,
userToken:null,
};
const loginReducer=(prevState,action)=>{
开关(动作类型){
案例“检索令牌”:
返回{
…国家,
userToken:action.token,
孤岛加载:false,
};
案例“登录”:
返回{
…国家,
用户名:action.id,
userToken:action.token,
孤岛加载:false,
};
“注销”案例:
返回{
…国家,
用户名:null,
userToken:null,
孤岛加载:false,
};
“登记册”一案:
返回{
…国家,
用户名:action.id,
userToken:action.token,
孤岛加载:false,
};
}
};
const[loginState,dispatch]=React.useReducer(loginReducer,initialLoginState);
const authContext=React.useMoom(()=>({
登录:(用户名、密码)=>{
//setUserToken('tony');
//设置加载(假);
让userToken;
userToken=null;
如果(用户名='user'&&password='pass'){
userToken='lexi';
}
分派({type:'LOGIN',id:userName,token:userToken});
},
注销:()=>{
//setUserToken(null);
//设置加载(假);
分派({type:'LOGOUT'});
},
注册:()=>{
setUserToken('tony');
设置加载(假);
},
}),[])
useffect(()=>{
设置超时(()=>{
//设置加载(假);
分派({type:'RETRIEVE_TOKEN',TOKEN:'mario'});
},1000);
},[]);
if(loginState.isLoading==true)
{
返回(
);
}
返回(
{loginState.userToken!==null(
}>
)
:
}
);
}
导出默认应用程序;

找到了错误。我改变了:

  dispatch({type:'LOGIN,',id:userName, token:userToken});
致:

  dispatch({type:'LOGIN',id:userName, token:userToken});