Version:
@react-native-community/async-storage = ^1.6.1
Problem:
- Importing AsyncStorage like this:
import { AsyncStorage } from '@react-native-community/async-storage'
- Using in my code like this:
AsyncStorage.setItem('locale', locale)
AsyncStorage.getItem('user').then((value) =>
{
...
}
Errors:
- TypeError: undefined is not an object (evaluating
'_asyncStorage.AsyncStorage.setItem')
- TypeError: undefined is not an object (evaluating '_asyncStorage.AsyncStorage.getItem')
What I've tried
- Importing
AsyncStorage from 'react-native' gives no problems, only warning that AsyncStorage should be imported from '@react-native-community' because the AsyncStorage from 'react-native' is deprecated.
- Tried to uninstall and reinstall '@react-native-community/async-storage'-dependency, that didn't work.
- Googling it
Full code:
import React from 'react';
import { View, Image, NativeModules } from 'react-native';
import { AsyncStorage } from '@react-native-community/async-storage'
import { styles } from '../../components/Styles.js';
import { GEOLocation } from '../../scripts/GEOLocation';
import Moment from 'moment/min/moment-with-locales';
export default class SplashScreen extends React.Component {
constructor(props) {
super(props);
this.geo = new GEOLocation();
this.setLocale();
this.bootstrapAsync();
}
bootstrapAsync = async () => {
this.geo.grantAccess()
AsyncStorage.getItem('user').then((value) => {
const user = JSON.parse(value);
this.props.navigation.navigate(user ? 'App' : 'Auth');
})
};
setLocale = () => {
const deviceLocale = NativeModules.I18nManager.localeIdentifier
var locale;
if (deviceLocale.includes('_')) {
var language = deviceLocale.split('_')[0]
var country = deviceLocale.split('_')[1].toLowerCase()
locale = language + '-' + country
} else {
locale = deviceLocale
}
if(Moment.locales().indexOf(locale) > -1 ) {
console.log('device locale')
AsyncStorage.setItem('locale', locale)
} else {
console.log('default locale')
AsyncStorage.setItem('locale', 'en')
}
}
render() {
return (
<View style={styles.container}>
<Image style={styles.leImage} source={require('../../../assets/logo_icon.png')} />
</View>
)
}
}