保存TextInput数组的状态 - React Native

问题描述 投票:1回答:1

我从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: '[email protected]',

    to: ['<[email protected]>'],

    signedByFullName: '',

    signedByCPRNumber: '',

    otherCompanyName: '',

    otherCompanyCVRNumber: ''
  })
})

}

react-native save state render textinput
1个回答
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>)

要么

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>)
© www.soinside.com 2019 - 2024. All rights reserved.