保存TextInput数组的状态-React Native

时间:2019-03-21 14:16:19

标签: react-native save state render textinput

我正在从API获取多个TextInput,并将其显示在屏幕上。因此,如何将用户输入的状态保存到全局对象。像这样:

    state = {
    foundtextfields: []
   }

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

var foundTextFields = [];

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

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

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

编辑: 我想遍历数组的状态,并从该ex中提取密钥。 “ key”(signedbyFullName),如果与JSON正文属性“ 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: ''
  })
})

}

1 个答案:

答案 0 :(得分:3)

要存储值,您可以在值的foundtextfields边创建一个数组,每次更改文本时,将其设置为另一个数组的索引
像这样:

    foundTextFields.push(
      <TextInput 
         onChangeText={(text) =>{
            let inputValues=this.state.inputValues;
            inputValues[i]=text;
            this.setState({inputValues})
         }}>
        {keyvalue_to_json.inputFields[i].placeholderText}
      </TextInput>)

OR

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>)