2017-09-18 3 views
0

アレイを保存しようとしましたが、ドキュメントに従おうとしましたが、悲惨に失敗しました。さまざまな警告やエラーを私に与えないように、どうすればいいですか?AsyncStorage Reactネイティブでアレイを保存する

エラー:私はアイテム

  • を設定しようとすると、

    • は、[オブジェクトオブジェクト]を得た
    • 専用のプロパティを読み取るために割り当てようとしました代わりに、配列
    • のオブジェクトを手に入れました期待される文字列、配列を持っています

    コードはです

    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}></TextInput> 
     
         </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}) 
     
        }

    エクストラ注:エラーが私の携帯電話に万博のApp上でAndroidとiOSの両方

    事前に感謝します!

  • +0

    'addNote()'が間違っています。 'this state'は不変であると考えるべきです。そのコピーを作成してコピーを修正してから、そのコピーを使って 'setState'を一度だけ変更してください。 –

    +0

    どうすればいいですか?私はいくつかのガイドやコードを教えてください。私はまだ –

    +1

    グーグルではまだノブですか?ここには[1](https://github.com/facebook/immutable-js/wiki/Immutable-as-React-state) –

    答えて

    1

    配列やその他のオブジェクトをAsyncStorageに文字列として保存する必要があります。両方の値を仮定し、

    は、入力値を持つ既存のキーの値をマージします:また、あなたが反応-ネイティブドキュメントからAsyncStorage.multiMerge

    を使用し、配列内の値を更新する必要がある場合

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

    文字列化されたJSONです。 Promiseオブジェクトを返します。

    +0

    私は示唆できる唯一の改善は、OPの(元のポスターの)コードのいくつかを含むことです彼は彼らが初心者であると述べたので、その例にどのように適合しているかを示します。 – ContinuousLoad

    関連する問題