javascript - AsyncStorage React Native 保存数组

标签 javascript arrays react-native expo asyncstorage

我尝试保存一个数组,我尝试遵循文档但惨败。我应该如何编写它,这样它才不会给我带来各种警告和错误。

错误:

  • 当我尝试设置项目时得到一个 [Object Object]
  • 得到一个对象而不是数组
  • 尝试分配给只读属性
  • 需要一个字符串,得到一个数组

这里是代码:App.js

 import React from "react";
 import {
  StyleSheet,
  Text,
  View,
  TextInput,
  ScrollView,
  TouchableOpacity,
  KeyboardAvoidingView,
  AsyncStorage
} from "react-native";
import Note from "./app/components/note";

export default class App extends React.Component {
 state = {
    noteArray: [],
    noteText: ""
};

render() {
    let notes = this.state.noteArray.map((val, key) => {
        return (
            <Note
                key={key}
                keyval={key}
                val={val}
                deleteMethod={() => this.deleteNote(key)}
            />
        );
    });

    return (
        <KeyboardAvoidingView behavior="padding" style={styles.container}>
            <View style={styles.header}>
                <Text style={styles.headerText}>Tasker</Text>
            </View>

            <ScrollView style={styles.scrollContainer}>{notes}</ScrollView>

            <View style={styles.footer}>
                <TouchableOpacity
                    onPress={this.addNote.bind(this)}
                    style={styles.addButton}
                >
                    <Text style={styles.addButtonText}>+</Text>
                </TouchableOpacity>

                <TextInput
                    style={styles.textInput}
                    placeholder="Enter Task..."
                    placeholderTextColor="white"
                    underlinedColorAndroid="transparent"
                    onChangeText={noteText => this.setState({ noteText })}
                    value={this.state.noteText}
                />
            </View>
        </KeyboardAvoidingView>
    );
}

addNote() {
    if (this.state.noteText) {
        var d = new Date();
        this.state.noteArray.push({
            date:
                d.getFullYear() +
                "/" +
                (d.getMonth() + 1) +
                "/" +
                d.getDate(),
            note: this.state.noteText
        });
        this.setState({ noteArray: this.state.noteArray });
        this.setState({ noteText: "" });
    }

    //AsyncStorage.setItem() How do I write it so no errors occur
    alert({ noteArray: this.state.noteArray });
}
}

额外注意:错误出现在我的 Android 和 iOS 手机上的 Expo 应用程序上

提前致谢!

最佳答案

数组和其他对象需要在AsyncStorage中保存为字符串。

AsyncStorage.setItem('arrayObjKey', JSON.stringify(myArray));

此外,如果您需要更新数组中的值,请使用AsyncStorage.multiMerge

来自 React-Native 文档:

Merges an existing key value with an input value, assuming both values are stringified JSON. Returns a Promise object.

关于javascript - AsyncStorage React Native 保存数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46272533/

相关文章:

arrays - 比较两个数组以检测删除和添加元素

android - Detox + React Native Android 启动到黑屏

javascript - 如何创建格式化的 javascript 控制台日志消息

javascript - 将照片 slider 放入 Canvas 标签内

c++ - 如何将类型别名设置为 "a pointer to an array of const int"?

javascript - React Native 导航 - 绘制选项 - 添加开关

android - React Native Expo生成apk返回错误: should NOT have additional property 'googleMobileAdsAppId'

javascript - 如何编辑我的代码以使用 jQuery 在切换时将一个 div 的内容添加(而不是替换)到另一个 div

javascript - 如何在我的 Web 应用程序中使用单一模式并避免在每个页面上重复 html?

C# select 不改变数组的值