2017-06-23 64 views
0

我想获取用户输入,从userInput创建一个对象数组,并将该数组对象保存到数组中。下面是我写的代码,但没有输出。从对象数组插入数据到阵列React Native

import React, { Component } from 'react'; 
import {Text, View, StyleSheet, TextInput, Image, TouchableOpacity, ListView} from 'react-native'; 
//import {Actions} from 'react-native-router-flux'; 

const count = 0; 

export default class SecondPage extends Component { 
constructor(props) { 
    super(props); 
    this.state = { 
     quan:'', 
     desc:'', 
     amt:'', 
     dataStorage :[], 
     data: { quantity: this.quan, description: this.desc, amount: this.amt }, 
    } 
    } 

_add(){ 
    console.log('Add button pressed'); 
    this.state.dataStorage[count].push(this.state.data); 
    console.log(this.state.data); 
    count++; 
} 
render(){ 
return(
<View style={styles.container}> 
      <View style={styles.itemDescription}> 
       <Text style={styles.itemDescriptionText}>QUANTITY</Text> 
       <Text style={styles.itemDescriptionText}>DESCRIPTION</Text> 
       <Text style={styles.itemDescriptionText}>AMOUNT</Text> 
       <TouchableOpacity style={styles.addButton} onPress={this._add}> 
       <Text style={styles.addButtonText}>ADD</Text> 
       </TouchableOpacity> 
      </View> 
      <View style={styles.rowsOfInput}> 
       <TextInput style = {styles.nameInput} 
         onChangeText={(text) => this.setState({quan: text})} 
         value = {this.state.quan} 
         autoCapitalize='none' 
         autoCorrect={false} 
         returnKeyType="next" 
         keyboardAppearance="dark" 
       /> 
       <TextInput style = {styles.nameInput} 
         onChangeText={(text) => this.setState({desc: text})} 
         value = {this.state.desc} 
         autoCapitalize='none' 
         autoCorrect={false} 
         returnKeyType="next" 
         keyboardAppearance="dark" 
       /> 
       <TextInput style = {styles.nameInput} 
         onChangeText= {(text) => this.setState({amt: text})} 
         value = {this.state.amt} 
         autoCapitalize='none' 
         autoCorrect={false} 
         returnKeyType="next" 
         keyboardAppearance="dark" 
       /> 

      </View>  
</View> 

)} 
} 


const styles = StyleSheet.create({ 
    container: { 
    flexDirection: 'column', 
    }, 
    itemDescription: { 
     marginTop:20, 
     backgroundColor:'#00CED1', 
     flexDirection:'row', 
     justifyContent:'space-between', 

    }, 

    itemDescriptionText:{ 
     fontSize:12, 
     color:'white', 
    }, 

    addButton:{ 
    borderWidth:1, 
    height:20, 
    borderRadius:5, 
    overflow:'hidden', 
    backgroundColor:'red', 

    }, 
    addButtonText:{ 
    paddingLeft:10, 
    paddingRight:10, 

    }, 
    nameInput:{ 
    flex:1, 
    height: 20, 
    textAlignVertical:'bottom', 
    paddingLeft: 5, 
    paddingRight: 5, 
    fontSize: 12, 
    backgroundColor:'#E0FFFF', 
    }, 
    hairLine:{ 
    height:1, 
    backgroundColor:'black', 
    marginTop:0, 
    marginLeft:20, 
    marginRight:20 
    }, 
    rowsOfInput:{ 
    // flex:1, 
     flexDirection:'row', 
     justifyContent:'space-around' 
    }, 
}); 

代码有什么问题?我想将每个条目的userInput存储为QUANTITY,DESCRIPTION,AMOUNT作为对象数组。

回答

1

你的问题之一是一个范围界定问题。将您的_add方法更改为此。

_add =() => { 
    let dataStorage = [{amt: this.state.amt, quan: this.state.quan, desc: this.state.desc}, ...this.state.dataStorage] 
    console.log(dataStorage) 
    this.setState({dataStorage}) 
} 

此外,您data财产上state永远不会工作,是不必要的。

这里是一个example

它仍然不会显示任何东西,因为你什么也没做,dataStorage

+0

感谢您澄清一些要点。我会努力的。 – Sameer