Search code examples
react-nativeinputsetsavetextinput

Setting input values on call function - React Native


I am having trouble with saving the value of text inputs(which i am fetching from an API) to an array. Currently I can save the values but if I try to edit one text input value, it will save the new value and still keep the old one as well. I want to save only the latest value when the 'onChangeText' has ended.

I appreciate any suggestion!

Here is my code:

textfieldsObject = () => {
    const obje = this.props.navigation.state.params.item;
    var keyvalue_to_json = JSON.parse(obje.keyValues);
    var textinputName = [];
    var foundTextFields = [];

    for (let i = 0; i < keyvalue_to_json.inputFields.length; i++) {
        if (keyvalue_to_json.inputFields[i].type === 'textfield') {
            foundTextFields.push(<TextInput onEndEditing={(e) => {               
                    keyvalue_to_json.inputFields[i].inputValues = e.nativeEvent.text;
                    this.myInputFields.myTextFields.push(keyvalue_to_json.inputFields[i])
                }}
            >{keyvalue_to_json.inputFields[i].placeholderText}</TextInput>)
        }
    }
    return (
            <View>
                {foundTextFields}
            </View>
    )
}

Solution

  • You can't make a push every time you edit your textinput. You will get an array of every edit you made, I think that's not what you want.
    Maybe this:

    this.myInputFields.myTextFields.[i]=keyvalue_to_json.inputFields[i]