AsyncStorage React Native 保存一个数组

我尝试保存一个数组,我尝试按照文档进行操作,但失败了.应该怎么写才不会给我各种警告和错误.

I tried saving an array, I tried to follow the documentation but failed miserably. How should I write it so that it doesn't give me various warnings and errors.

错误:

  • 当我尝试设置项目时得到一个 [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 App 上

Extra Note : The Error is on Expo App on my phone both Android and iOS

提前致谢!

推荐答案

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

Arrays and other objects need to be saved as strings in AsyncStorage.

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

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

Also if you need to update the values in the array use AsyncStorage.multiMerge

来自 React-Native 文档:

From the React-Native docs:

将现有键值与输入值合并,假设两个值都是字符串化的 JSON.返回一个 Promise 对象.

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

相关文章