我正在从API中获取多个TextInputs,并在我的屏幕上显示它们。因此,如何将用户输入状态保存到全局对象。这样的东西:
state = {
foundtextfields: []
}
在这里,我将这些获取的TextInput推向Founttextfields []数组:
var foundTextFields = [];
foundTextFields.push(<TextInput>{keyvalue_to_json.inputFields[i].placeholderText}</TextInput>)
我正在此列表中显示文本输入:
return (
<View>
{foundtextfields}
</View>
)
编辑:我想循环遍历数组的状态,然后从该ex中提取键。"钥匙"(签名)并匹配下面的JSON Body属性" signedbyfullname"。
postToBmp = (obje) => {
var userArray = [];
for (i = 0; i < this.myInputFields.myTextFields.length; i++) {
userArray.push(this.myInputFields.myTextFields[i])
console.log("this is the title of al inputFields:", this.myInputFields.myTextFields[i].key)
}
fetch('URL', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'Connection': 'Keep-Alive',
},
credentials: 'include',
body: JSON.stringify({
from: 'test1@test.dk',
to: ['<test@hotmail.com>'],
signedByFullName: '',
signedByCPRNumber: '',
otherCompanyName: '',
otherCompanyCVRNumber: ''
})
})
}
要存储可以使用值沿侧面foundtextfields
创建数组的值,并且每次更改文本时,都将其设置为其他数组的索引
喜欢:
foundTextFields.push(
<TextInput
onChangeText={(text) =>{
let inputValues=this.state.inputValues;
inputValues[i]=text;
this.setState({inputValues})
}}>
{keyvalue_to_json.inputFields[i].placeholderText}
</TextInput>)
或
foundTextFields.push(
<TextInput
onChangeText={(text) =>{
keyvalue_to_json=this.state.keyvalue_to_json
keyvalue_to_json.inputFields[i].inputValues=text;
this.setState({keyvalue_to_json})
}}>
{keyvalue_to_json.inputFields[i].placeholderText}
</TextInput>)