TypeError: undefined is not an object (evaluating '_asyncStorage.AsyncStorage.setItem')
TypeError: undefined is not an object (evaluating '_asyncStorage.AsyncStorage.setItem')
版本:
@react-native-community/async-storage = ^1.6.1
问题:
- 像这样导入 AsyncStorage:
import { AsyncStorage } from '@react-native-community/async-storage'
- 在我的代码中这样使用:
AsyncStorage.setItem('locale', locale)
AsyncStorage.getItem('user').then((value) =>
{
...
}
错误:
- TypeError: undefined 不是一个对象(正在评估
'_asyncStorage.AsyncStorage.setItem')
- TypeError: undefined 不是对象(正在评估“_asyncStorage.AsyncStorage.getItem”)
我试过的
- 从 'react-native' 导入
AsyncStorage
没有问题,只是警告 AsyncStorage
应该从 '@react-native-community' 导入,因为 AsyncStorage
来自 'react-native' 已弃用。
- 尝试卸载并重新安装“@react-native-community/async-storage”依赖项,但没有成功。
- 谷歌搜索
完整代码:
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>
)
}
}
下面是如何使用正确的导入方法。
import AsyncStorage from '@react-native-community/async-storage';
此模块未导出为 react-native
,因此它不能有方括号。
在react-native
模块中使用
import { AsyncStorage } from 'react-native';
@react-native-community/async-storage 现已弃用
使用react-native-async-storage/async-storage
相反。
注意:不使用括号。不像以前它是从 react-native 导出的许多东西之一,它现在是 @react-native-async-storage/async-storage.
的默认导出
import AsyncStorage from '@react-native-async-storage/async-storage';
版本:
@react-native-community/async-storage = ^1.6.1
问题:
- 像这样导入 AsyncStorage:
import { AsyncStorage } from '@react-native-community/async-storage'
- 在我的代码中这样使用:
AsyncStorage.setItem('locale', locale)
AsyncStorage.getItem('user').then((value) =>
{
...
}
错误:
- TypeError: undefined 不是一个对象(正在评估 '_asyncStorage.AsyncStorage.setItem')
- TypeError: undefined 不是对象(正在评估“_asyncStorage.AsyncStorage.getItem”)
我试过的
- 从 'react-native' 导入
AsyncStorage
没有问题,只是警告AsyncStorage
应该从 '@react-native-community' 导入,因为AsyncStorage
来自 'react-native' 已弃用。 - 尝试卸载并重新安装“@react-native-community/async-storage”依赖项,但没有成功。
- 谷歌搜索
完整代码:
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>
)
}
}
下面是如何使用正确的导入方法。
import AsyncStorage from '@react-native-community/async-storage';
此模块未导出为 react-native
,因此它不能有方括号。
在react-native
模块中使用
import { AsyncStorage } from 'react-native';
@react-native-community/async-storage 现已弃用
使用react-native-async-storage/async-storage
相反。
注意:不使用括号。不像以前它是从 react-native 导出的许多东西之一,它现在是 @react-native-async-storage/async-storage.
的默认导出import AsyncStorage from '@react-native-async-storage/async-storage';