【问题标题】:AsyncStorage React Native save an ArrayAsyncStorage React Native 保存一个数组
【发布时间】:2017-09-18 06:04:10
【问题描述】:

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

错误:

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

提前致谢!

【问题讨论】:

  • addNote() 是错误的。您应该认为this state 是不可变的。复制一份并修改副本,然后setState 只复制一次。
  • 我该怎么做?请你能给我一些指南或代码吗?我还是个菜鸟
  • 谷歌 ?这里是one

标签: javascript arrays react-native expo asyncstorage


【解决方案1】:

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

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

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

来自 React-Native 文档:

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

【讨论】:

  • 我可以建议的唯一改进是包含一些 OP(原始海报的)代码以显示它如何适合他们的示例,因为他/她提到他们是初学者。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-09
相关资源
最近更新 更多