版本:
@react-native-community/async-storage = ^1.6.1问题:
import { AsyncStorage } from '@react-native-community/async-storage'AsyncStorage.setItem('locale', locale)
AsyncStorage.getItem('user').then((value) =>
{
...
}错误:
我试过的
AsyncStorage”导入AsyncStorage没有任何问题,只是警告说AsyncStorage应该从‘@react本机-社区’导入,因为从‘react-原生’导入的AsyncStorage是不推荐的。完整代码:
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>
)
}
}发布于 2019-07-30 13:16:43
下面是如何使用正确的导入方法。
import AsyncStorage from '@react-native-community/async-storage';此模块不作为react-native导出,因此不能有方括号。
用于react-native模块
import { AsyncStorage } from 'react-native';发布于 2021-11-23 20:14:11
@react本机-社区/异步存储现在不再受欢迎
而不是。
注:不使用括号。与以前不同的是,它是从react本地导出的许多东西之一,现在它是@react-本机-异步存储/异步存储的默认导出。
import AsyncStorage from '@react-native-async-storage/async-storage';发布于 2022-09-01 11:46:44
是的,我的问题是通过以下方式解决的
import AsyncStorage from '@react-native-async-storage/async-storage';在此之前,我使用括号并得到错误。
https://stackoverflow.com/questions/57272535
复制相似问题