即使按照文档中的方式调用和实现,也会发生本机异步存储渲染错误
react native async storage render error occurred even though it was called and implemented as it is in the documents
ERROR TypeError: undefined is not an object (evaluating
'_$$_REQUIRE(_dependencyMap[6],
"@react-native-async-storage/async-storage").AsyncStorage.getItem')
This error is located at:
in App (at renderApplication.js:48)
in RCTView (at View.js:32)
in View (at AppContainer.js:106)
in RCTView (at View.js:32)
in View (at AppContainer.js:133)
in AppContainer (at renderApplication.js:41)
Blockquote
my code:`
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;
`
如 the docs 中所示,AsyncStorage
应作为默认导入而不是命名导入导入。
所以替换:
import { AsyncStorage } from '@react-native-async-storage/async-storage';
作者:
import AsyncStorage from '@react-native-async-storage/async-storage';
ERROR TypeError: undefined is not an object (evaluating '_$$_REQUIRE(_dependencyMap[6], "@react-native-async-storage/async-storage").AsyncStorage.getItem')
This error is located at: in App (at renderApplication.js:48) in RCTView (at View.js:32) in View (at AppContainer.js:106) in RCTView (at View.js:32) in View (at AppContainer.js:133) in AppContainer (at renderApplication.js:41)
Blockquote my code:`
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;
`
如 the docs 中所示,AsyncStorage
应作为默认导入而不是命名导入导入。
所以替换:
import { AsyncStorage } from '@react-native-async-storage/async-storage';
作者:
import AsyncStorage from '@react-native-async-storage/async-storage';