【问题标题】:TypeError: undefined is not an object (evaluating '_asyncStorage.AsyncStorage.setItem')TypeError:未定义不是对象(评估'_asyncStorage.AsyncStorage.setItem')
【发布时间】:2019-12-07 21:32:16
【问题描述】:

版本:

@react-native-community/async-storage = ^1.6.1

问题:

  1. 像这样导入 AsyncStorage:
import { AsyncStorage } from '@react-native-community/async-storage'
  1. 在我的代码中这样使用:
AsyncStorage.setItem('locale', locale)

AsyncStorage.getItem('user').then((value) => 
{
...
}

错误:

  1. TypeError:未定义不是对象(评估 '_asyncStorage.AsyncStorage.setItem')
  2. TypeError: undefined is not an object(评估 '_asyncStorage.AsyncStorage.getItem')

我尝试过的

  1. 从 'react-native' 导入 AsyncStorage 没有问题,只是警告应该从 '@react-native-community' 导入 AsyncStorage,因为不推荐使用来自 'react-native' 的 AsyncStorage
  2. 尝试卸载并重新安装“@react-native-community/async-storage”-dependency,但没有成功。
  3. 谷歌搜索

完整代码:

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>
    )
  }
}

【问题讨论】:

    标签: react-native asyncstorage


    【解决方案1】:

    以下是如何使用正确的导入方法。

    import AsyncStorage from '@react-native-community/async-storage';
    

    此模块未导出为react-native,因此它不能有方括号。

    react-native模块中使用

    import { AsyncStorage } from 'react-native';
    

    【讨论】:

    • 谢谢,我知道我在从 'react-native' 转换为 '@react-native-community' 时忽略了一些事情。
    • @hong 我把它放在花括号里我仍然收到错误.. 有什么想法吗?
    • @Varun 你为@react-native-async-storage/async-storage 安装了吗??
    【解决方案2】:

    @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';
    
    

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 2021-12-01
      • 2019-11-16
      • 2020-02-09
      • 2022-01-01
      • 2019-01-20
      • 2014-06-13
      • 2021-07-24
      • 2016-09-02
      相关资源
      最近更新 更多