Javascript 将数据从对象数组插入到本机数组

Javascript 将数据从对象数组插入到本机数组,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我试图获取用户输入,从userInput创建一个对象数组,并将该对象数组保存到一个数组中。下面是我编写的代码,但没有输出 import React, { Component } from 'react'; import {Text, View, StyleSheet, TextInput, Image, TouchableOpacity, ListView} from 'react-native'; //import {Actions} from 'react-native-router

我试图获取用户输入,从userInput创建一个对象数组,并将该对象数组保存到一个数组中。下面是我编写的代码,但没有输出

    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'
    },
});
import React,{Component}来自'React';
从“react native”导入{Text,View,StyleSheet,TextInput,Image,TouchableOpacity,ListView};
//从“react native router flux”导入{Actions};
常数计数=0;
导出默认类SecondPage扩展组件{
建造师(道具){
超级(道具);
此.state={
泉:",,
描述:'',
金额:'',
数据存储:[],
数据:{数量:this.quan,说明:this.desc,金额:this.amt},
}
}
_添加(){
console.log(“按下添加按钮”);
this.state.dataStorage[count].push(this.state.data);
console.log(this.state.data);
计数++;
}
render(){
返回(
量
描述
数量
添加
this.setState({quan:text})
值={this.state.quan}
自动资本化=“无”
自动更正={false}
returnKeyType=“下一步”
键盘外观=“深色”
/>
this.setState({desc:text})}
值={this.state.desc}
自动资本化=“无”
自动更正={false}
returnKeyType=“下一步”
键盘外观=“深色”
/>
this.setState({amt:text})
值={this.state.amt}
自动资本化=“无”
自动更正={false}
returnKeyType=“下一步”
键盘外观=“深色”
/>
)}
}
const styles=StyleSheet.create({
容器:{
flexDirection:'列',
},
项目说明:{
玛金托普:20,
背景颜色:“#00CED1”,
flexDirection:“行”,
辩护内容:'space-between',
},
ItemDescriptionContext:{
尺寸:12,
颜色:'白色',
},
添加按钮:{
边框宽度:1,
身高:20,
边界半径:5,
溢出:'隐藏',
背景颜色:'红色',
},
addButtonText:{
paddingLeft:10,
paddingRight:10,
},
名称输入:{
弹性:1,
身高:20,
textAlignVertical:“底部”,
paddingLeft:5,
paddingRight:5,
尺寸:12,
背景颜色:“#E0FFFF”,
},
发际线:{
身高:1,,
背景颜色:'黑色',
玛金托普:0,
marginLeft:20,
marginRight:20
},
行输入:{
//弹性:1,
flexDirection:“行”,
为内容辩护:“周围空间”
},
});

代码中有什么错误?我想将每个条目的用户输入存储到QUANTITY、DESCRIPTION和AMOUNT中,作为对象数组。

您的问题之一是范围问题。更改您的
\u将
方法添加到此

_add = () => {
  let dataStorage = [{amt: this.state.amt, quan: this.state.quan, desc: this.state.desc}, ...this.state.dataStorage]
  console.log(dataStorage)
  this.setState({dataStorage})
}
另外,您的
状态
上的
数据
属性将永远不会工作,并且是不必要的

这是一个例子

它仍然不会显示任何内容,因为您不使用
数据存储