ERROR TypeError: undefined不是一个对象(求值)_ $ $ _REQUIRE (_dependencyMap [6],"@react-native-async-storage/async-storage") .AsyncStorage.getItem")
此错误位于:在App中(at renderApplication.js:48)在RCTView中(at View.js:32)in View (at AppContainer.js:106)在RCTView中(at View.js:32)in View (at AppContainer.js:133)在AppContainer (at renderApplication.js:41)
引用我的代码:
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import OnboardingScreen from './Component/Onboardingscreen';
import LoginScreen from './Component/Loginscreen';
import { AsyncStorage } from '@react-native-async-storage/async-storage';
import { useEffect } from 'react';
const AppStack = createStackNavigator();
const App =() => {
const [isFirstLaunch, setIsFirstLaunch,] = React.useState(null);
useEffect(() => {
AsyncStorage.getItem('alreadyLaunched').then(value => {
if(value == null) {
AsyncStorage.setItem('alreadyLaunched','true');
setIsFirstLaunch(true);
} else {
setIsFirstLaunch(false);
}
});
}, []);
if (isFirstLaunch == null) {
return null;
} else if ( isFirstLaunch == true ){
return (
<NavigationContainer>
<AppStack.Navigator
headerMode='none'
>
<AppStack.Screen name="OnboardingScreen" component={OnboardingScreen}/>
<AppStack.Screen name='login' component={LoginScreen} />
</AppStack.Navigator>
</NavigationContainer>
);
} else {
return <LoginScreen />;
}
}
export default App;
如文档中所示,AsyncStorage
应该作为默认导入而不是命名导入导入。
替换:
import { AsyncStorage } from '@react-native-async-storage/async-storage';
由:
import AsyncStorage from '@react-native-async-storage/async-storage';