javascript - expo AsyncStorage 方法不是函数

标签 javascript node.js reactjs react-native expo

我是 React 原生环境的新手,我正在尝试构建一个应用程序,我在其中使用 AsyncStorage 来保存用户首选项。

我无法保存/获取任何东西并且低于警告,例如,

[Unhandled promise rejection: TypeError: _reactNative.default.getItem is not a function. (In '_reactNative.default.getItem('name')', '_reactNative.default.getItem' is undefined)]

Stack trace:
  src\components\Settings\Settings.js:62:39 in _callee$
  node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch
  node_modules\regenerator-runtime\runtime.js:271:30 in invoke
  node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch
  node_modules\regenerator-runtime\runtime.js:135:28 in invoke
  node_modules\regenerator-runtime\runtime.js:170:17 in <unknown>
  node_modules\promise\setimmediate\core.js:45:7 in tryCallTwo
  node_modules\promise\setimmediate\core.js:200:23 in doResolve
  node_modules\promise\setimmediate\core.js:66:12 in Promise
  node_modules\regenerator-runtime\runtime.js:169:27 in callInvokeWithMethodAndArg
  node_modules\regenerator-runtime\runtime.js:192:38 in enqueue
  node_modules\regenerator-runtime\runtime.js:216:8 in async
  src\components\Settings\Settings.js:60:16 in _callee
  src\components\Settings\Settings.js:13:2 in componentDidMount
  node_modules\react-native\Libraries\Renderer\oss\ReactNativeRenderer-dev.js:15036:10 in commitLifeCycles
  node_modules\react-native\Libraries\Renderer\oss\ReactNativeRenderer-dev.js:16636:8 in commitAllLifeCycles

注意:我在 setItem 和 getItem 方法中都收到此警告....

loadDefaults = async () => {
        AsyncStorage.setItem('name','TEST');
        let val = await AsyncStorage.getItem('name');
        console.log("val >  " + val);

我的 package.json 文件,

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "expo": "^35.0.0",
    "expo-haptics": "^7.0.0",
    "react": "16.8.3",
    "react-dom": "16.8.3",
    "react-native": "https://github.com/expo/react-native/archive/sdk-35.0.0.tar.gz",
    "react-native-circular-progress": "^1.3.3",
    "react-native-easy-grid": "^0.2.2",
    "react-native-gesture-handler": "^1.3.0",
    "react-native-reanimated": "^1.3.0",
    "react-native-svg": "^9.13.3",
    "react-native-web": "^0.11.7",
    "react-navigation": "^4.0.10",
    "react-navigation-drawer": "^2.3.2",
    "react-navigation-stack": "^1.10.3",
    "react-navigation-tabs": "^2.5.6"
  },
  "devDependencies": {
    "babel-preset-expo": "^7.1.0"
  },
  "private": true
}

完整的组件代码库

import React from 'react';
import { View, Text,Switch, StyleSheet, Platform, Alert } from 'react-native';
import { TextInput, TouchableOpacity } from 'react-native-gesture-handler';
import { colors } from '../../themes';
import AsyncStorage from 'react-native';


export default class Settings extends React.Component {

    componentDidMount() {
        this.loadDefaults();
    }

    constructor(props) {

        super(props);
        this.state = {
            input:''
        };

        this.savePref = this.savePref.bind(this);
        this.onTxtChange = this.onTxtChange.bind(this);

        this.styles = StyleSheet.create({
            container: {
                //paddingTop: 23
            },
            text: {
                marginLeft:20,
                margin: 10
            },
            input: {
                marginLeft: 10,
                marginRight: 10,
                height: 40,
                borderColor: colors.primary,
                borderWidth: 1,
                padding: 10,
                paddingLeft:20,
                borderRadius:10

            },
            submitButton: {
                backgroundColor: colors.primary,
                padding: 10,
                margin: 10,
                height: 40,
                borderRadius:10
            },
            switchContainer: {
                flexDirection:'row'
            }
        });
    }

    loadDefaults = async () => {
        AsyncStorage.setItem('name','TEST');
        let val = await AsyncStorage.getItem('name');
        console.log("XXXXXX >  " + val);
    }



    onTxtChange = (val) => {
        this.setState({input: val});
    }

    savePref = () => {
        AsyncStorage.setItem("input", this.state.input);
        this.showAlert();
    }

    showAlert = () => {
        Alert.alert(  
            'Preferences',  
            'Your preferences have been saved.',  
            [
                {text: 'OK'},  
            ],  
            {cancelable: false}  
        );
    }

    render() {
        return (
            <View style={this.styles.container}>

                <Text style={this.styles.text}>Input: </Text>

                <TextInput style={this.styles.input}
                    placeholder="Enter input"
                    placeholderTextColor={colors.grayFontColor}
                    keyboardType="number-pad"
                    value={this.state.input}
                />
                <TouchableOpacity
                    style={this.styles.submitButton}
                    onPress={this.savePref}
                    >
                    <Text style={{alignSelf:'center', color: '#fff'}}> Save </Text>
                </TouchableOpacity>
            </View>
        );
    }
}

最佳答案

您正在默认导入 AsyncStorage。您需要为此使用大括号。

替换为:

import AsyncStorage from 'react-native';

有了这个:

import { AsyncStorage } from 'react-native';

关于javascript - expo AsyncStorage 方法不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58618640/

相关文章:

javascript - vue.js - craftyjs 点击不起作用

javascript - Node zlib——内部应用程序数据库中字符串的 Gzip vs Deflate vs DeflateRaw

javascript - 在 Express 中设置默认响应 header

javascript - 更新 Firebase 中的所有子项

javascript - VS Code - 使 jsx intellisense 适用于简单的 ".js"文件

javascript - IE 8 的 window.open() 方法 - 强制窗口在选项卡之外打开

javascript - 获取另一页输入框的值

javascript - 使用嵌套对象和默认值进行解构

javascript - Node Js如何等待forEach执行并获取计数结果

reactjs - 导出不带 'export default' 的高阶组件