【发布时间】:2020-01-12 13:02:05
【问题描述】:
我使用for循环设置数组值,但我只是获取数据值的最后一个输入值。也就是说,console.log(data) 只输出最后一个值。
导出默认类 App 扩展 React.Component { 从'react'导入*作为React; 从 'react-native' 导入 { Text, View, StyleSheet,TextInput,TouchableOpacity };
state={
form:{
name:'',
country:'',
age:''
}
}
onButton=()=>{
const data = {};
for(let element in this.state.form){
data[element] = this.state.form[element]
}
console.log(data)
}
render() {
return (
<View style={styles.container}>
<View style={{marginTop:100,width:'100%'}}>
<TextInput
style={styles.input}
value={this.state.form.name}
onChangeText={(name)=>this.setState({form:{name}})}
/>
<TextInput
style={styles.input}
value={this.state.form.country}
onChangeText={(country)=>this.setState({form:{country}})}
/>
<TextInput
style={styles.input}
value={this.state.form.age}
oonChangeText={(age)=>this.setState({form:{age}})}
/>
</View>
<TouchableOpacity onPress={()=>this.onButton()} style={{borderColor:'green',borderWidth:1,padding:10,marginTop:50}}>
<Text>GONDER</Text>
</TouchableOpacity>
</View>
);
} }
【问题讨论】:
-
我不明白这个问题,你能简要解释一下你面临的问题是什么吗?
-
那个“看起来你的帖子主要是代码;请添加更多细节。”是有原因的。
标签: javascript reactjs react-native