保存 TextInput 数组的状态 - React Native

Save state of TextInput array - React Native

我正在从 API 多个 TextInputs 中获取并将它们显示在我的屏幕上。那么,如何将用户输入的状态保存到全局对象中。像这样:

    state = {
    foundtextfields: []
   }

在这里,我将那些获取的 TextInputs 推送到 foundTextFields[] 数组:

var foundTextFields = [];

    foundTextFields.push(<TextInput>{keyvalue_to_json.inputFields[i].placeholderText}</TextInput>)

我在此列表中显示文本输入:

return (
    <View>
      {foundtextfields}
    </View>
)

编辑: 我想遍历数组的状态,并从那个 ex 中提取密钥。 “key”(signedbyFullName),如果与 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>)