1

I am fetching from API multiple TextInputs and displaying them on my screen. So, how can I save the state of user inputs to a global object. Something like this:

    state = {
    foundtextfields: []
   }

Here I am pushing those fetched TextInputs to foundTextFields[] array:

var foundTextFields = [];

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

And I am displaying text inputs in this list:

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

EDIT: I want to loop through the state of the array, and extract the key from that ex. “key” (signedbyFullName) and match if that is the same with the json body property "signedbyFullName" like below.

  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: ''
  })
})

}

4
  • 1
    What is the problem can you display the TextInputs or just need help to store the values? Commented Mar 21, 2019 at 15:01
  • @MPN7 I just need help to store the values. In fact I tried this to store values: this.state.foundtextfields.push(…) and call in view {this.state.foundtextfields}. It works this way but I am using a Modal and whenever I toggle it, textinputs duplicates. I dont know why! Commented Mar 21, 2019 at 15:17
  • 1
    Where are you doing this this.state.foundtextfields.push(…)? if you are doing it in the Modal it self it's normal that it duplicates Commented Mar 21, 2019 at 15:23
  • @MPN7 Nono modal is inside another function's return View. Still, it doesnt make sense why is it doing it. It seems like everytime I toggle Modal, it refreshes the page, thus my fetch() calls data again + data that are already saved in state, therefor they are so many displaying. Commented Mar 21, 2019 at 15:27

1 Answer 1

2

To store the values you can create an array along side foundtextfields with the values, and every time you change a text you set it to the index of the other array
Like so:

    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>)
Sign up to request clarification or add additional context in comments.

10 Comments

If I want to loop through that array in the State, how can I identify to which TextInput does each value belongs to?
Cicle through your API array keyvalue_to_json.inputFields, the values of input are in the same index as that array. If that confuses you, see edit i set the input values to the keyvalue_to_json.inputFields so it has all values
Because you are not initializing inputFields anywhere, and you are not setting state.
Maybe you misunderstood. I want to loop through state of that array, from another function. I have a function called postToApi() and there I want to loop through the state. Check Edited question.
I'm not sure if I understood but let me explain: The second way uses the variable of your array json that came from your api call. From the code you provided I believe you are doing an loop and for each element of keyvalue_to_json.inputFields you create a new inputBox, so instead of creating a new variable I set the values of the inputs to the original, api variable (keyvalue_to_json.inputFields), I don't set state 'cus I thought that keyvalue_to_json.inputFields wasn't on state since you were having problems with state.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.