<tfoot id='nqr2Z'></tfoot>
  • <small id='nqr2Z'></small><noframes id='nqr2Z'>

      <bdo id='nqr2Z'></bdo><ul id='nqr2Z'></ul>

      <legend id='nqr2Z'><style id='nqr2Z'><dir id='nqr2Z'><q id='nqr2Z'></q></dir></style></legend>

        <i id='nqr2Z'><tr id='nqr2Z'><dt id='nqr2Z'><q id='nqr2Z'><span id='nqr2Z'><b id='nqr2Z'><form id='nqr2Z'><ins id='nqr2Z'></ins><ul id='nqr2Z'></ul><sub id='nqr2Z'></sub></form><legend id='nqr2Z'></legend><bdo id='nqr2Z'><pre id='nqr2Z'><center id='nqr2Z'></center></pre></bdo></b><th id='nqr2Z'></th></span></q></dt></tr></i><div id='nqr2Z'><tfoot id='nqr2Z'></tfoot><dl id='nqr2Z'><fieldset id='nqr2Z'></fieldset></dl></div>

        AsyncStorage React Native 保存一个数组

        AsyncStorage React Native save an Array(AsyncStorage React Native 保存一个数组)

        <i id='XKpjO'><tr id='XKpjO'><dt id='XKpjO'><q id='XKpjO'><span id='XKpjO'><b id='XKpjO'><form id='XKpjO'><ins id='XKpjO'></ins><ul id='XKpjO'></ul><sub id='XKpjO'></sub></form><legend id='XKpjO'></legend><bdo id='XKpjO'><pre id='XKpjO'><center id='XKpjO'></center></pre></bdo></b><th id='XKpjO'></th></span></q></dt></tr></i><div id='XKpjO'><tfoot id='XKpjO'></tfoot><dl id='XKpjO'><fieldset id='XKpjO'></fieldset></dl></div>
      1. <tfoot id='XKpjO'></tfoot>

            <bdo id='XKpjO'></bdo><ul id='XKpjO'></ul>

            <small id='XKpjO'></small><noframes id='XKpjO'>

                <tbody id='XKpjO'></tbody>
              <legend id='XKpjO'><style id='XKpjO'><dir id='XKpjO'><q id='XKpjO'></q></dir></style></legend>

                  本文介绍了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.

                  这篇关于AsyncStorage React Native 保存一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Gradient Button to background-color blinks on hover(悬停时背景颜色的渐变按钮闪烁)
                  How to make hovering over active button not use hover effect?(如何使悬停在活动按钮上不使用悬停效果?)
                  html javascript show image hover(html javascript 显示图像悬停)
                  How to get css hover values on click?(如何在点击时获得 css 悬停值?)
                  Change background image on link hover(更改链接悬停时的背景图像)
                  Highlight multiple items on hover#39;s condition(突出显示悬停条件下的多个项目)

                  <small id='4r9q0'></small><noframes id='4r9q0'>

                  • <i id='4r9q0'><tr id='4r9q0'><dt id='4r9q0'><q id='4r9q0'><span id='4r9q0'><b id='4r9q0'><form id='4r9q0'><ins id='4r9q0'></ins><ul id='4r9q0'></ul><sub id='4r9q0'></sub></form><legend id='4r9q0'></legend><bdo id='4r9q0'><pre id='4r9q0'><center id='4r9q0'></center></pre></bdo></b><th id='4r9q0'></th></span></q></dt></tr></i><div id='4r9q0'><tfoot id='4r9q0'></tfoot><dl id='4r9q0'><fieldset id='4r9q0'></fieldset></dl></div>
                        <bdo id='4r9q0'></bdo><ul id='4r9q0'></ul>
                        <tfoot id='4r9q0'></tfoot>

                          <legend id='4r9q0'><style id='4r9q0'><dir id='4r9q0'><q id='4r9q0'></q></dir></style></legend>
                            <tbody id='4r9q0'></tbody>