gpt4 book ai didi

react-native - 类型错误 : undefined is not an object (evaluating '_asyncStorage.AsyncStorage.setItem' )

转载 作者:行者123 更新时间:2023-12-01 19:56:18 30 4
gpt4 key购买 nike

版本:

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

问题:

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

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

    错误:

    1. 类型错误:未定义不是一个对象(正在评估'_asyncStorage.AsyncStorage.setItem')
    2. 类型错误:未定义不是对象(正在计算“_asyncStorage.AsyncStorage.getItem”)

    我尝试过的

    1. 从“react-native”导入 AsyncStorage 不会出现任何问题,只是警告应从“@react-native-community”导入 AsyncStorage,因为 'react-native' 中的 AsyncStorage 已弃用。
    2. 尝试卸载并重新安装“@react-native-community/async-storage”依赖项,但没有成功。
    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>
    )
    }
    }

    最佳答案

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

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

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

    react-native模块中使用

    import { AsyncStorage } from 'react-native';

    关于react-native - 类型错误 : undefined is not an object (evaluating '_asyncStorage.AsyncStorage.setItem' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57272535/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com